I am currently working on a Ruby on Rails project which shows a list of images.
A must-have for this project is that it shows new posts in realtime without the need of refreshing the web page. After searching for a while, I've stumbled upon some JavaScript solutions and services such as PubNub; however, none of the provided solutions made sense at all.
In the JavaScript solution (polling) the following happens:
- User 1 views the list of photos.
- In the background the JavaScript code is polling an endpoint every second to see if there is a new post.
- User 2 adds a new photo.
- There is a delay of 50 ms before the new cycle is triggered and fetches the new data.
- The new content is loaded in the DOM.
This seems odd when translated to a real world example:
- User 1 holds a pile of pictures on his/her desk.
- He/she walks to the photographer every second and asks if he has a new one.
- The photographer makes a new photo.
- This second when he/she walks in, she can take the picture and put it on the pile.
In my opinion the solution should be as following:
- User 1 holds a pile of pictures on his/her desk.
- The photographer takes a new picture.
- The photographer walks to the pile and puts it with the rest.
The PubNub solution is basically the same, however this time there is an intern walking between the parties to share the data.
Needless to say, both solutions are very energy consuming as they are triggered even when there is no data to load.
As far as my knowledge goes there is no (logic) explanation why this way of implementation is used in almost every realtime application.