Pushing data from Server to Client without a client request

clientserver

I've noticed while browsing Stack Exchange that there are dynamic notifications like "3 new messages, click to show". I want to have this kind of dynamic updating for what I'm about to explain.

Let's say I want to create a carousel/slideshow of 10 recent news articles. I want this carousel to be updated hourly, in a queue. Newer articles will push older articles out of the queue. The solution off the top of my head would be.

  1. User logs on to client.
  2. Client calculates # of minutes until next hour mark and sets a timer to execute at the hour.
  3. At hour mark, send a request to the server about any new news articles that haven't been in the carousel already.
  4. Handle response.
  5. Reset timer.

Is this an acceptable strategy? Can I achieve this without relying on client requests? In other words how does Stack Exchange achieve its dynamic updating?

Best Answer

In order to push data, you'd have to identify a Client, and that would be done by subscribing the client to the server. Once that's done you'd have a list of subscribed client with a persistent connection.

Depending on what you want to achieve, I'd say it's best for the client to request to the server, so that you don't have to maintain a persistent connection and make use of request/response communication protocols as HTTP.

One example that comes to mind to keep a persistent connection would be a live chat / instant messaging system, as the communication should happen real time.

Keep in mind persistent connection are usually implemented through the use of sockets, which adds an overlay in implementing your own communication protocol, encryption, etc...

Related Topic