本文翻译自:What are Long-Polling, Websockets, Server-Sent Events (SSE) and 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: 有人想向我解释一下这些技术是什么吗?
- Long Polling 长轮询
- Server-Sent Events 服务器发送的事件
- Websockets 网络套接字
- 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? 我已经听到很多关于websockets的信息(使用socket.io [一个node.js库]),但是为什么不使用PHP?
#1楼
参考:https://stackoom.com/question/kTr7/什么是长轮询-Websocket-服务器发送事件-SSE-和Comet
#2楼
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. 在理解这些技术之前,您必须首先了解经典的 HTTP Web流量。
Regular HTTP: 常规HTTP:
- A client requests a webpage from a server. 客户端从服务器请求网页。
- The server calculates the response 服务器计算响应
- The server sends the response to the client. 服务器将响应发送到客户端。
Ajax Polling: Ajax轮询:
- A client requests a webpage from a server using regular HTTP (see HTTP above). 客户端使用常规HTTP(请参见上面的HTTP)从服务器请求网页。
- The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at regular intervals (eg 0.5 seconds). 客户端接收请求的网页,并在页面上执行JavaScript,该页面以固定间隔(例如0.5秒)从服务器请求文件。
- The server calculates each response and sends it back, just like normal HTTP traffic. 服务器会计算每个响应并将其发送回,就像正常的HTTP流量一样。
Ajax Long-Polling: Ajax长轮询:
- A client requests a webpage from a server using regular HTTP (see HTTP above). 客户端使用常规HTTP(请参见上面的HTTP)从服务器请求网页。
- The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server. 客户端接收所请求的网页,并在从服务器请求文件的页面上执行JavaScript。
- The server does not immediately respond with the requested information but waits until there's new information available. 服务器不会立即响应所请求的信息,而是等待直到有新的信息可用。
- When there's new information available, the server responds with the new information. 当有新信息可用时,服务器将以新信息进行响应。
- The client receives the new information and immediately sends another request to the server, re-starting the process. 客户端接收到新信息,并立即向服务器发送另一个请求,以重新启动该过程。
HTML5 Server Sent Events (SSE) / EventSource: HTML5服务器发送事件(SSE)/ EventSource:
- A client requests a webpage from a server using regular HTTP (see HTTP above). 客户端使用常规HTTP(请参见上面的HTTP)从服务器请求网页。
- The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server. 客户端接收请求的网页,并在该页面上执行JavaScript,以打开与服务器的连接。
The server sends an event to the client when there's new information available. 当有新信息可用时,服务器会将事件发送给客户端。
- Real-time traffic from server to client, mostly that's what you'll need 从服务器到客户端的实时流量,这正是您所需要的
- You'll want to use a server that has an event loop 您将要使用具有事件循环的服务器
- Connections with servers from other domains are only possible with correct CORS settings 只有使用正确的CORS设置,才能与其他域的服务器建立连接
- If you want to read more, I found these very useful: (article) , (article) , (article) , (tutorial) . 如果您想阅读更多,我发现这些非常有用:( 文章) , (文章) , (文章) , (教程) 。
HTML5 Websockets: HTML5 Websockets:
- A client requests a webpage from a server using regular http (see HTTP above). 客户端使用常规http(请参见上面的HTTP)从服务器请求网页。
- The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server. 客户端收到请求的网页,并在该网页上执行JavaScript,以打开与服务器的连接。
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. 使用WebSockets,可以与来自另一个域的服务器连接。
- It is also possible to use a third party hosted websocket server, for example Pusher or others . 也可以使用第三方托管的websocket服务器,例如Pusher或其他 。 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 ). 如果您想阅读更多,我发现这些非常有用:( 文章 ), (文章) ( 教程 )。
Comet: 彗星:
Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Comet是HTML5之前的技术的集合,这些技术使用流和长轮询来实现实时应用程序。 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 ? 我已经听到很多关于websockets的信息(使用socket.io [一个node.js库]),但是为什么不使用PHP?
You can use PHP with WebSockets, check out Ratchet . 您可以将PHP与WebSockets一起使用,请查看Ratchet 。
#3楼
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. Tieme为他的出色答案付出了很多努力,但是我认为OP问题的核心是这些技术如何与PHP相关联,而不是每种技术如何工作。
PHP is the most used language in web development besides the obvious client side html, css, and javascript. 除了明显的客户端html,css和javascript,PHP是Web开发中最常用的语言。 Yet PHP has 2 major issues when it comes to real time applications: 然而,对于实时应用程序,PHP存在两个主要问题:
1) PHP started as a very basic CGI. 1)PHP最初是一个非常基本的CGI。 PHP has progressed very far since it's early stage, but it happened in small steps. 从早期开始,PHP就已经取得了长足的进步,但是它发生的步伐很小。 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. 到今天PHP成为可嵌入且灵活的C库时,PHP已经拥有数百万用户,其中大多数人都依赖于它的早期执行模型,因此它还没有做出坚定的尝试来逃避PHP。内部cgi模型。 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. 甚至命令行界面也会调用PHP库(在Linux上为libphp5.so,在Windows上为php5ts.dll等),就好像它还是处理GET / POST请求的cgi一样。 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. 结果,它几乎不支持多线程或事件驱动的编程(在PHP用户空间内),从而使其目前对于实时的多用户应用程序不可行。
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. 请注意,PHP确实具有扩展,可以在PHP用户空间中提供事件循环(例如libevent)和线程(例如pthreads),但是很少有应用程序使用它们。
2) PHP still has significant issues with garbage collection. 2)PHP在垃圾回收方面仍然存在重大问题。 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. 尽管这些问题一直在不断改善(如上所述,这可能是结束生命周期的最重要的一步),但即使是创建长期运行的PHP应用程序的最佳尝试,也需要定期重新启动。 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. PHP 7也将是解决这些问题的重要一步,并且作为用于实时应用程序的平台似乎很有希望。
#4楼
I have tried to make note about these and have collected and written examples from a java perspective . 我试图记下这些内容,并从Java角度收集并编写了示例。
HTTP for Java Developers 适用于Java开发人员的HTTP
Reverse Ajax - Old style 反向Ajax-旧样式
Async Handling on server side 服务器端的异步处理
Reverse Ajax - New style 反向Ajax-新样式
Putting it here for any java developer who is looking into the same subject. 对于正在研究同一主题的任何Java开发人员,请放在此处。
#5楼
You can easily use Node.JS in your web app only for real-time communication. 您可以轻松地将Web应用程序中的Node.JS仅用于实时通信。 Node.JS is really powerful when it's about WebSockets. 关于WebSocket,Node.JS确实非常强大。 Therefore "PHP Notifications via Node.js" would be a great concept. 因此,“通过Node.js进行PHP通知”将是一个很好的概念。
See this example: Creating a Real-Time Chat App with PHP and Node.js 请参见以下示例: 使用PHP和Node.js创建实时聊天应用程序