javascriptphpajaxlong-pollingajax-polling

Long Polling or full-duplex connection? Where is the catch?


I'm implementing a messaging system with long poll to have real time updates for my users. Doing so I noticed that some websites such as Hotmail use also xhr requests but they seem to be a little different from the one I have implemented.

As you can see in the picture, in my implementation the client makes a request, the server holds the request until new data updates are available. Then sends back the payload and closes the connection. Once received, javascript sends a new request to the web server.

Hotmail instead sends the request back while leaving the connection open. HOW is this possible?? And how can I implement this myself? And most important, what's the difference?

Thank you.

Long-poll


Solution

  • Hotmail uses web sockets. Web sockets requests are different from http request in a way that a web socket connection is persistent all the time while the user is on the website. So the communication goes like this in shortly: The first time the user opens your website he sends http request to your server. Since the http protocol is on the 7th layer of the OSI Model, the request goes through the TCP layer and establishes a socket connection (which is on the 5th layer of OSI Model) with the server. Using the appropriate server-side web sockets technology, the server can use this persistent socket connection with the client to push data when it's needed.

    Depending on what language are you using for your back end you can use several different technologies/libraries for implementing web sockets. For asp.net web application you could use Microsoft's SignalR. If you use javscript (Node.js) for your back-end than you can use Socket.io. Note that you can also use Socket.io even if you are not using node.js for your back-end but the implementations won't be as trivial because you would now have 2 different severs which will will potentially need to share data (Ex: sessions or database).

    The good thing about Node.js and SignalR is that they are very performant and very scalable to many users, especially Node.js. Another great feature of both this technologies is that if the client's browser doesn't support web sockets they have appropriate fallback methods, like Server Sent Events, Ajax polling, Ajax long polling, Hidden iframe element...

    For further reading I recommend this Stackoverflow question