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 -
- Long Polling
- Server-Sent Event
- Websockets
- 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.
1 Answer
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:
- A client requests a webpage from a server.
- The server calculates the response
- The server send the response to the client.
AJAX Polling:
- A client requests a webpage from a server use regular http (see http above).
- The requested webpage executes javascript which requests a file from a server at the regular intervals (e.g. 0.5 seconds).
- The server calculate each response and sends it back just like normal http traffic.
AJAX Long-Polling:
- A client requests a webpage from a server use regular http (see http above).
- he requested webpage executes javascript which requests a file from a server.
- The server does not immediately responds with the requested information but waits until there's newinformation available.
- When there's new information available the server responds with the new information.
- The clients receives the new information and immediately sends another request to the server re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
- A client requests a webpage from a server use regular http (see http above).
- The requested webpage executes javascript which opens a connection with the server.
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 Websockets:
- A client requests a webpage from a server use regular http (see http above).
- The requested webpage executes javascript which opens a connection with the server.
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.
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.
Source:
http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet
- What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
- WebSockets VS Server-Sent Events VS Long-polling
- WebSockets vs Server-Sent Events vs Long-polling
- WebSockets VS Server-Sent Events VS Long-polling
- WebSockets vs Server-Sent Events vs Long-polling
- WebSockets VS Server-Sent Events VS Long-polling
- Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别
- Long-Polling, Websockets, SSE(Server-Sent Event), WebRTC 之间的区别
- SSE(Server-sent Events)
- 从long polling 到 websockets
- Comet:基于 HTTP 的long-polling技术
- websocket server-sent polling 对比
- Bosh vs Comet vs Long Polling vs Polling
- server-sent events使用
- SSE(Server-Sent Events) 服务端向浏览器推送消息的实现
- The Future of Comet: Part 2, HTML 5’s Server-Sent Events
- Tutorial: JSF 2 and HTML5 Server Sent Events
- Django push: Using Server-Sent Events and WebSocket with Django
- oracle 11.2.0.3 set memory_target 3/4 of physical memory on rhel x86_64
- 画弧线的解释
- 我创建的service怎么在4.0以上跑步起来呢?
- Bash ---- [ and [[ memo
- gson使用在android使用例子
- What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?
- How to Add Splash Screen in Your iOS App
- cocos2d-x android多分辨率适配
- 无法安装VMware-workstation-full的解决办法
- Visio怎么插入二次方
- ubuntu 13.04 链接局域网共享
- 设计模式:桥梁模式
- css/html备忘录
- UVA 132 Bumpy Objects【凸包变形】【附图】