7 minutes read

Do you know how your social network knows when you receive a message from a friend and immediately sends you a notification about it? Most likely, the social platform uses a persistent connection with the server. It does not close the connection after each message and is always ready to receive another message. In this topic, we will dive into web sockets and a few other technologies that provide a persistent connection between the client and the server.

Persistent connection

A persistent connection is also known as HTTP keep-alive or HTTP connection reuse. It is a single and always-open HTTP connection that can send multiple requests and receive multiple responses.

multiple connections versus persistent connections

Persistent connections were first used in 1996 in HTTP version 1.0. Since HTTP version 1.1 allows reusing a connection for multiple requests and responses, it was necessary to add an additional header to the request:

Connection: keep-alive

The connection will be open until either the client or the server sends the close header:

Connection: close

Since HTTP version 1.1, all connections are persistent unless declared otherwise.

Persistent connections have a lot of advantages over connections that close after each request/response:

  • The latency in sending requests to the server becomes less because you do not need to open a new connection every time.

  • Using a persistent connection, it is possible to send or receive multiple requests/responses at the same time.

  • The connection will not close in case an error occurs. This increases the stability of the connection.

Of the many protocols available, each is unique and provides a different set of features. Let's look at how you can upgrade to protocols that may better fit your needs.

Protocol upgrading

HTTP 1.1 (or a higher version) also provides protocol upgrading for persistent connections. It can be used to upgrade HTTP 1.1 to HTTP 2.0 or an HTTP connection into a WebSocket. This can be useful when the browser or server only supports a specific protocol or a connection via WebSocket is required. The following headers must be set to upgrade the protocol:

Connection: upgrade
Upgrade: protocol/version

You can upgrade multiple protocols by listing them one by one and separating them by a comma.

The protocol version is optional. You may not specify the protocol version.

Here is an example of upgrading HTTP 1.1 to HTTP 2.0:

Connection: upgrade
Upgrade: HTTP/2.0

And upgrading it into a WebSocket can be done this way:

Connection: upgrade
Upgrade: websocket

Types of persistent connections

There are many different ways of establishing a persistent connection. We will talk about the three most popular among them: Long Polling, Server-Sent Events (SSE), and WebSockets. Each type has its advantages and is suitable for different situations.

Let's start with Long Polling — the oldest technology and the easiest way to create a persistent connection. The name of this technology speaks for itself. In simple terms, the browser establishes an HTTP connection with the server and holds it for a long time so that the server can send its response at any time without having to create a new connection.

Another way to create a persistent connection is by using Server-Sent Events (SSE). SSE is a technology based on the HTTP protocol that allows the browser to receive automatic updates from the server. Basically, the server sends updates to the browser and changes the content on the web page asynchronously. SSE is quite simple technology, so it is supported by all browsers except Internet Explorer. Server-Sent Events can be a good solution for small applications where a persistent connection is needed.

Now it's time to talk about some of the most used technology cases where you need a persistent connection between the server and the browser — WebSockets. The WebSocket protocol is an independent protocol based on the TCP, unlike long polling and SSE which are based on HTTP. It enables closer interaction between the browser and the website, facilitating the distribution of interactive content and the creation of real-time applications. Since WebSocket is a fairly lightweight protocol, it allows up to 1024 parallel connections from the browser. This is more connections than either long polling or SSE can support. WebSocket also has its own URI scheme because it is an independent protocol. WebSocket has two URI schemes: ws:// for insecure and wss:// for secure connections.

Always opt for wss://. This ensures the security of transmitted data and the reliability of your application.

WebSockets has a lot of functions and is both fast and safe. Because of these features, it is used in many applications by popular IT companies. This protocol can be used in many cases like e-commerce websites, social networks, multiplayer video games, and more.

Conclusion

Now you know a little more about how computer networks work. You studied persistent connections and technologies that provide such connections. In this topic, you became familiar with technologies such as Long Polling, Server-Sent Events (SSE), and WebSockets. Let's outline some key facts:

  • Persistent connections are connections that open once and can process as many requests as needed.

  • Since HTTP version 1.1, it is possible to change the protocol version or the protocol itself.

  • There are three ways to provide persistent connections; using Long Polling, Server-Sent Events, or WebSockets.

  • WebSockets is an independent protocol while Long Polling and SSE are based on HTTP.

Let's revise everything you have learned with some practical tasks.

94 learners liked this piece of theory. 3 didn't like it. What about you?
Report a typo