0

Following this post : In what situations would AJAX long/short polling be preferred over HTML5 WebSockets?

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 !

Community
  • 1
  • 1
Louis V
  • 181
  • 1
  • 12
  • Your clients wouldn't request the data, they would idle on the socket waiting for data to be emitted from the server, which would then be pushed to the client in real-time. – Ben Fortune Aug 14 '14 at 14:45
  • OK, so very second the Nodejs server woiuld push to the varnish the new Data, and the varnish would push all the new data to the clients, all at a time ? It is ok even if I have more than 2K people connected at the same time on the website ? – Louis V Aug 14 '14 at 14:51
  • I don't have any experience with varnish so I don't know how it'd work with websockets, though looking [here](http://www.thatsgeeky.com/2012/03/websockets-varnish-nginx-and-node-js/) it should do exactly what you want it to do. – Ben Fortune Aug 14 '14 at 14:53

1 Answers1

0

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.

Clarence
  • 2,944
  • 18
  • 16