Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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?

like image 533
user1437328 Avatar asked Jun 18 '12 06:06

user1437328


People also ask

What is long polling in WebSocket?

Long polling takes HTTP request/response polling and makes it more efficient, since repeated requests to a server wastes resources. For example, establishing a new connection, parsing the HTTP headers, a query for new data, response generation and delivery, and finally connection closure and clean up.

What is the difference between long polling WebSockets and Server-Sent Events?

Obviously, the major difference between WebSockets and Server-Sent Events is that WebSockets are bidirectional (allowing communication between the client and the server) while SSEs are mono-directional (only allowing the client to receive data from the server).

Does SSE use WebSockets?

Use cases for SSE and WebSocket SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill.

What is long polling?

HTTP Long Polling is a technique used to push information to a client as soon as possible on the server. As a result, the server does not have to wait for the client to send a request. In Long Polling, the server does not close the connection once it receives a request from the client.


4 Answers

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.

    • 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
    • If you want to read more, I found these very useful: (article), (article), (article), (tutorial).

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.

like image 150
Tieme Avatar answered Oct 14 '22 02:10

Tieme


Tieme put a lot of effort into his excellent answer, but I think the core of the OP's 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 its 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 its earlier model of execution, so it hasn't yet made a solid attempt to escape the CGI model internally. Even the command line 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 its 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.

  1. PHP still has significant issues with garbage collection. Although these issues have been consistently improving (likely its 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 makes 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.

like image 34
JSON Avatar answered Oct 14 '22 03:10

JSON


Polling

Basically, polling is a technique of requesting the information from the server after regular intervals. This connection happens by following HTTP protocol. There are two types of polling:

  1. Short Polling
  2. Long Polling

Short Polling

In short polling, the client requests information from the server. The server processes the request. If data is available for the request, server responds to the request with the required information. However, if the server has no data available for the client, server returns an empty response. In both the situation, the connection will be closed after returning the response. Clients keep issuing new requests even after server sends the empty responses. This mechanism increases the network cost on the server.

Long polling

In long polling, the clients can request information from the server with the expectation that the server may not respond immediately. When the server receives the request, if it has no fresh data for the client, rather than returning an empty response, the server keeps the request open and waits for data to arrive. When the server receives new data, it delivers the response to the client right away, completing the open request. The client can then send another request for new updates after getting the answer from the server. Long polling reduces costs by reducing the number of empty responses.

WebSocket

WebSocket is a protocol that provides two-way(bi-directional) communication channels over a single TCP connection. Websocket facilitates a persistent connection between a client and a server, allowing both parties to begin transferring data at any moment. The WebSocket handshake is the procedure through which the client creates a WebSocket connection. If the operation is successful, the server and client can send and receive data at any time. Mostly used in real-time web applications such as WhatsApp, Uber.

Server-sent event (SSE)

Unlike WebSockets, we can not issue requests from a client to a server using SSE since it's a one-way connection. When we require "near real-time" transmission from the server to the client, or if the server generates data in a loop, SSE is the ideal choice.

Comet

Comet is a web application design paradigm that describes a continuous, two-way interaction between a server and a web browser using native HTTP methods. Comet is an umbrella term. Ajax Push, HTTP Streaming, and HTTP Server Push are some of the HTTP mechanisms that may be used to provide this event-driven interaction.

like image 25
Gunjan Avatar answered Oct 14 '22 02:10

Gunjan


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

like image 33
Supun Kavinda Avatar answered Oct 14 '22 02:10

Supun Kavinda