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

1 篇文章 0 订阅


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


up vote 41 down vote accepted

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值