dongqi3533
dongqi3533
2012-06-18 06:28
浏览 95
已采纳

什么是长轮询,Websockets,服务器发送事件(SSE)和Comet?

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 to me what these technologies are:

  1. Long Polling
  2. Server-Sent Events
  3. Websockets
  4. Comet

One thing that I came across every time 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 the client use the data, maybe some code might help?)

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

图片转代码服务由CSDN问答提供 功能建议

我试过阅读一些文章,但我对这些概念还不是很清楚。

有人愿意尝试向我解释这些技术是什么:

  1. 长轮询
  2. 服务器 - 发送的事件
  3. Websockets
  4. Comet

    我每次遇到的一件事是, 服务器保持连接打开并将数据推送到客户端。 如何保持连接打开,客户端如何获取推送数据? (客户端如何使用数据,也许某些代码可能有帮助?)

    现在,我应该将哪一个应用于实时应用。 我听过很多关于websockets的事情(有socket.io [一个node.js库]),但为什么不用PHP?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douzhou7037
    douzhou7037 2012-10-12 08:57
    已采纳

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

    Before you can understand these 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 sends the response to the client.

    HTTP

    Ajax Polling:

    1. A client requests a webpage from a server using regular HTTP (see HTTP above).
    2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at regular intervals (e.g. 0.5 seconds).
    3. The server calculates 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 using regular HTTP (see HTTP above).
    2. The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
    3. The server does not immediately respond with the requested information but waits until there's new information available.
    4. When there's new information available, the server responds with the new information.
    5. The client 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 using regular HTTP (see HTTP above).
    2. The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
    3. The server sends an event to the client when there's new information available.

    HTML5 SSE

    HTML5 Websockets:

    1. A client requests a webpage from a server using regular http (see HTTP above).
    2. The client receives the requested webpage and executes the JavaScript on the page 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.

      • Real-time traffic from the server to the client and from the client to the server
      • You'll want to use a server that has an event loop
      • 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 these very useful: (article), (article) (tutorial).

    HTML5 WebSockets

    Comet:

    Comet is a collection of techniques prior to HTML5 which use 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 WebSockets, check out Ratchet.

    点赞 评论
  • dongweishi2028
    dongweishi2028 2014-10-14 07:11

    Tieme put a lot of effort into his excellent answer, but I think the core of the OPs question is how these technologies relate to PHP rather than how each technology works.

    PHP is the most used language in web development besides the obvious client side html, css, and javascript. Yet PHP has 2 major issues when it comes to real time applications:

    1) PHP started as a very basic CGI. PHP has progressed very far since it's early stage, but it happened in small steps. PHP already had many millions of users by the time it became the embed-able and flexible C library that it is today, most of whom were dependent on it's earlier model of execution, so it hasn't yet made a solid attempt to escape the cgi model internally. Even the commandline interface invokes the PHP library (libphp5.so on linux, php5ts.dll on windows, etc) as if it still a cgi processing a GET/POST request. It still executes code as if it just has to build a "page" and then end it's life cycle. As a result, it has very little support for multi-thread or event driven programming (within PHP userspace), making it currently unpractical for real time, multi-user applications.

    Note that PHP does have extensions to provide event loops (such as libevent) and threads (such as pthreads) in PHP userspace, but very, very, few of the applications use these.

    2) PHP still has significant issues with garbage collection. Although these issues have been consistently improving (likely it's greatest step to end the life cycle as described above), even the best attempts at creating long running PHP applications require being restarted on a regular basis. This also make it unpractical for real time applications.

    PHP 7 will be a great step to fix these issues as well, and seems very promising as a platform for real-time applications.

    点赞 评论
  • dongwei8729
    dongwei8729 2017-04-21 14:47

    I have tried to make note about these and have collected and written examples from a java perspective.

    HTTP for Java Developers

    Reverse Ajax - Old style

    Async Handling on server side

    Reverse Ajax - New style

    Server Sent Events

    Putting it here for any java developer who is looking into the same subject.

    点赞 评论
  • dongshi1868
    dongshi1868 2019-04-14 16:38

    You can easily use Node.JS in your web app only for real-time communication. Node.JS is really powerful when it's about WebSockets. Therefore "PHP Notifications via Node.js" would be a great concept.

    See this example: Creating a Real-Time Chat App with PHP and Node.js

    点赞 评论

相关推荐