Making our Application Real Time with Web Sockets

If you thought our application was real time until now, you would be wrong. Currently, our application gives us the appearance of being instant, but in reality, this is just the browser polling the server for information every once in a while (in our case, once in a while meant two seconds in the previous chapters).

While you may be thinking that this is okay since there is rarely any actual need to know the temperature or humidity with an urgency greater than a few seconds, there is still another problem that we haven't considered, and that is the large amount of useless data that is passed around because of this method of getting information. In a majority of cases, the temperature or humidity is stable (as far as our desired accuracy of a single decimal point is concerned) for much longer than a few seconds and changes only once every few minutes. This means that a lot of the AJAX calls that we make to our server just tell us that our readings haven't changed.

One of the solutions to this problem is to just increase the waiting time between API calls so that it is more reflective of the actual average time period between which readings change. However, if we do this, we do it at the cost of being instantaneous. Suddenly, we are faced with the choice of being perfectly instantaneous and perfectly efficient with our data but not both.

Fortunately for us, there is still a way in which we could solve this problem: what if the server could notify the client when there were any changes instead of the client constantly asking the server after every fixed time interval? This is exactly what we hope to achieve with the help of web sockets and, consequently, make our application truly real-time.

In this chapter, we will cover the following topics:

  • Web sockets
  • Implementing web sockets in our application
  • Creating our socket implementation in our application server
  • Client-side implementation
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.148