3

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

4

3 回答 3

1

There are two most common Bidirectional HTTP mechanisms, see RFC6202 "Known Issues and Best Practices for the Use of Long Polling and Streaming in Bidirectional HTTP":

  • HTTP Long Polling: The server attempts to "hold open" (not immediately reply to) each HTTP request, responding only when there are events to deliver. In this way, there is always a pending request to which the server can reply for the purpose of delivering events as they occur, thereby minimizing the latency in message delivery.

  • HTTP Streaming: The server keeps a request open indefinitely; that is, it never terminates the request or closes the connection, even after it pushes data to the client.

You can also find detailed list of issues of those approaches in the RFC6202. Each of those methods has its advantages and disadvantages.

So, during HTTP streamming the connection is not terminated:

The basic life cycle of an application using HTTP streaming is as follows:

  1. The client makes an initial request and then waits for a response.

  2. The server defers the response to a poll request until an update is available, or until a particular status or timeout has occurred.

  3. Whenever an update is available, the server sends it back to the client as a part of the response.

  4. The data sent by the server does not terminate the request or the connection. The server returns to step 3.

于 2015-10-10T17:24:52.953 回答
1

I think there is maybe a better approach than using long polling. You can implement WebSocket connections. Then you have a persistent, bidirectional connection to the server. WebSocket is an upgrade protocol based on HTTP and was built to avoid "kind-of-workaround"-tricks like long polling. If you want to read about this more detailed:

https://www.rfc-editor.org/rfc/rfc6455

http://www.html5rocks.com/en/tutorials/websockets/basics/

Just in case you want to support older browsers, that can't establish WebSockets, you can use something like Dojox/Socket that automatically use long polling as a fallback.

http://dojotoolkit.org/api/1.10/dojox/socket.html

https://www.sitepen.com/blog/2012/11/05/dojo-websocket-with-amd/

于 2015-10-10T18:24:43.443 回答
1

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

于 2015-10-10T19:23:29.277 回答