Intive Blog

WebSockets vs. Long Polling

Originally, web applications were built on a client-server model in which the one starting all transactions was always the first. Although it is easy to implement, this mechanism has a weakness: it does not allow the server to send information on its own.

I suggest we think of applications with information in real time (you will see that the old method would not adapt to this logic) and discover now Long Polling and WebSockets features.

 

Long Polling

The Long Polling technique allows us to emulate a “server push” feature over HTTP in which the server, as opposed to the old technique, “starts” events.

1

How does this technique work?

The client starts a request, opening a connection with the server until it is answered. Then, the server keeps that query open until having a new event and answers back with the information it has at the moment. When receiving the answer, the client places another query, keeping again the connection open and repeating the cycle.

Pros and cons of using Long Polling

Among the pros, we highlight:

  • It is an easy technique to implement
  • It is similar to events that, triggered by the server, turn it into a highly active participant in communication.

As cons we can mention:

  • Applications of intensive processing may find latency in continuous transmission to the client.
  • It must be setup in a way that connections do not stay alive permanently, this may cause constant re-connections.
  • Accumulation of processing can be generated in the queue and in the server, resulting in intensive use of resources and information loss.

 

WebSockets

WebSocket is a protocol that provides full-duplex communication channels when data travel both ways simultaneously. When a client-server communication is not this way, because the client makes a query and waits for a sever response, Websocket data are sent right away without waiting for a response.

2webs

How does this protocol work?

The client establishes a TCP connection through a “handshake”. Both the server and the client are capable of reciprocally sending messages anytime. Thus, the connection stays alive until any of the two explicitly finishes it.

The “handshake” (the communication channel parameter negotiation process between a client and a server) dealt with before is the following:

3webs

A basic example using the Websocket API:

In most cases, we will use libraries that allow us to implement Websocket technology to a higher level. For instance: Socket.io, WebSocket-Node, Faye.
In most cases, we will use libraries that allow us to implement Websocket technology to a higher level. For instance: Socket.io, WebSocket-Node, Faye.

 

Pros and cons of using WebSockets

Some of the pros are:

  • It eliminates latency problems that may arise from Long Polling.
  • Keeps a unique connection open.
  • No headers are sent, such as cookies, in every interchange.
  • It can work on port 80 and no additional set up is required (e.g. firewall).

Some cons are:

    • Most popular browsers, or their older versions, do not support this technology.
    • It requires an explicit action to close connection.
    • It generates great bandwidth consumption since an active connection is maintained throughout communication.

 

Conclusion

The decision whether to use WebSockets or Long Polls will largely depend on the resources available and the scalability of the application. Most libraries mentioned before share a common strategy that when failing to connect through WebSockets, (either because of connection problems or browsers that do not support this technology) they resort to Long Polling as a fallback. Many applications currently use WebSockets: games, social network or chats such as Slack, among others. We can consequently state that this technology will –surely- be on the rise in the forthcoming years.

We hope we have been clear enough and that you find all these useful when making your future choices!

Mariano Furriel

He’s been a developer in the company since March 2016 . He is studying the Computer Engineering degree at UADE. He is always attentive to read and learn about new technologies to improve the application designs.

Add comment