I am trying to build a real time notifications module that functions in a similar manner to facebook's realtime notifications for example if a user requests friendship of another user a push notification is sent to the second one where I have the following constraints: 1- notifications are sent to a specific user's all browser instances 2- user need to be logged in to subscribe him to notifications 3- not to use pooling because of performance issues 4- it obviously need to be secure
I did a quick research and I found out that the options I have are web sockets or server-sent events. server-sent events handles reconnecting automatically and quite fits the functionality I needed so I decided to go with it.
I built a sinatra event driven server where whenever a user requests friendship of another user the notification should be pushed through it to the second user but there appeared to be several problems where my solutions to it convinced me that I am going in the wrong direction.
How do I bind an eventsource to a specific user and at the same time maintain security. What is the proper way I can create an eventsource if and only if a user is logged in.
I also need general helpful guidelines to address this task
Usually you don't need to have "exactly real time" and you can use setTimeout(simulate_realtime, 5000); that checks if there is new data on the server each 5 seconds.
You can also checkout https://github.com/rails/actioncable that will be in Rails 5, which will be release a stable this fall. You can bet on it, it is the next main component that will be added to rails.
Probably you need eventmachine(https://github.com/eventmachine/eventmachine/tree/master/examples/guides/getting_started). Look at the chat example