HTML 5 WebSocket vs Ajax for numerous parallel connexions

Following this post : HTML5 WebSocket vs Long Polling vs AJAX vs WebRTC

I consider using WebSockets for my application, which I could describe as something like a real time stock price listing. I've read that I could seriously lower the charge of my front servers this way. So just "thinking" to it.

Presently : Every client calls my application every 1 second to get a JSON of datas. Those data are the same for all the users. So every second, one of the frontal Varnish I have calls for a new resources, eg it passes the request to the backend server (a tomcat) that generates a fresh JSON, and then passes it to the varnish with a TTL of 1second. And during 1 second, every client that with ask for the data will take it from the Varnish.

This allow the backend server to be relatively low charged with request, because the varnish take the majority of it.

My question : If now I come to WebSockets (WS) (I may switch from Tomcat to Nodejs for those requests), and my clients requests every second the fresh data.

  • Howwould I be able to generate the data for the "first" client, and serves it for all the other clients during the next second ?
  • How will I be able to say to the Varnish that this data has a TTL of 1s ?

Thank you in advance for you explanations !

Varnish is not an important part in the efficiency of websockets. The only thing varnish would do is to pipe the connection to the backend (for instance node.js). So nothing will be cached in varnish for the web sockets. Varnish could however still be useful in the picture when it comes to load balancing.

Each client/visitor would start a websocket connection when they enter the page/site. Then whenever you have new information (stock rates are updated?) you push that data to every connection that is open. So at what time interval it is refreshed is determined solely by when you node.js application pushes information.

This means you do not have to push information more often than there actually is new information available and could also mean that you can provide accurate sub-second information without overloading the server.