What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

来源:互联网 发布:日本文学推荐 知乎 编辑:程序博客网 时间:2024/05/16 05:08


Ok, I have tried reading some articles, but I am not very clear on the concepts yet.

Would someone like to take a shot at explaining me what these technologies are -

  1. Long Polling
  2. Server-Sent Event
  3. Websockets
  4. Commet

One thing that I came across everytime was, the server keeps a connection open and pushes data to the client. How is the connection kept open, and how does the client get the pushed data (how does it use basically, maybe some code might help?) ?

Now, which one of them should I use for a realtime app (that I need to code). I have been hearing a lot about websockets (with socket.io [a node.js library]) but why not PHP ?

Thanks in Advance.

share|improve this question
 
This is not 1 Q but a whole bunch of, much too broad/general, part of which you can answer yourself by doing research. I'll take on just 1: PHP. It simply wasn't designed for running networking services that keep massive numbers of TCP connections concurrently open and active (WebSocket). Thus, you need a framework that scales .. uses asynchronous / event based / reactor based design. – oberstet Jun 18 '12 at 8:33
 
You can find a general introduction to the terminology in this answer: stackoverflow.com/questions/10782058/… – Alessandro Alinone Jun 18 '12 at 18:38

1 Answer

activeoldestvotes

up vote41down voteaccepted

In the examples below the client is the browser, the server the webserver hosting the website.

Before you can understand those technologies, you have to understand classic http web traffic first.

Regular http:

  1. A client requests a webpage from a server.
  2. The server calculates the response
  3. The server send the response to the client.

HTTP

AJAX Polling:

  1. A client requests a webpage from a server use regular http (see http above).
  2. The requested webpage executes javascript which requests a file from a server at the regular intervals (e.g. 0.5 seconds).
  3. The server calculate each response and sends it back just like normal http traffic.

AJAX Polling

AJAX Long-Polling:

  1. A client requests a webpage from a server use regular http (see http above).
  2. he requested webpage executes javascript which requests a file from a server.
  3. The server does not immediately responds with the requested information but waits until there's newinformation available.
  4. When there's new information available the server responds with the new information.
  5. The clients receives the new information and immediately sends another request to the server re-starting the process.

AJAX Long-Polling

HTML5 Server Sent Events (SSE) / EventSource:

  1. A client requests a webpage from a server use regular http (see http above).
  2. The requested webpage executes javascript which opens a connection with the server.
  3. The server sends an event to the client when there's new information available.

    • Realtime traffic from server to client, mostly that's what you'll need.
    • You'll want to use a server that has an eventloop, don't use apache.
    • Not possible to connect with a server from another domain.

    • If you want to read more, I found thse (article), (article), (article), (tutorial) very useful.

HTML5 SSE

HTML5 Websockets:

  1. A client requests a webpage from a server use regular http (see http above).
  2. The requested webpage executes javascript which opens a connection with the server.
  3. The server and the client can now send each other messages when new data (on either side) is available.

    • Realtime traffic from the server to the client and from the client to the server.
    • You'll want to use a server that has an eventloop, don't use apache.
    • With WebSockets it is possible to connect with a server from another domain.

    • It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to implement the client side, which is very easy!

    • If you want to read more, I found thse (article), (article) (tutorial) very useful.

HTML5 WebSockets

Comet:

Comet is a collection of techniques prior to HTML5 which uses streaming and long-polling to achieve real time applications. Read more on wikipedia or this article.


Now, which one of them should I use for a realtime app (that I need to code). I have been hearing a lot about websockets (with socket.io [a node.js library]) but why not PHP ?

You can use php with webwebsockets, check out Ratchet.

share|improve this answer
 
2 
thanks for working so hard really! this answer is awesome. – user1437328 Oct 13 '12 at 11:56
 
No problem, you're welcome! – Tieme Oct 15 '12 at 13:38
 
This is awesome! I am reading up on SSE and found this article, it's very nice - like I've now compared stuff, can you also include SSE here so we can also cross-check it's difference with Websocket? – index Nov 7 '12 at 7:34
 
Read it again, SSE (HTML5 Server Side Events) is included ;-) – Tieme Nov 7 '12 at 9:30
 
@Tieme Oh was that it? I thought SSE meant Server-Sent Events. Anyway, thanks, I see it now. – index Nov 16 '12 at 3:28
show 1 more comment

Source: 

http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet


原创粉丝点击