What are the possible cases of using a persistent connection?

Asked

Viewed 422 times

5

I’ve been reading about Signalr for the last few days and basically realized that one of its main features is to maintain a persistent server connection with possible clients accessing the server. Basically, it allows us to create Hubs and then allows several clients (which can be both Web and . NET) applications connect to these Hubs and there is a kind of continuous connection.

I’ve been working with web programming since 2009 and this is quite different from the traditional approach using stateless HTTP. In these cases everything is based on requests and answers. The client makes a request, the request is processed in a pipeline on the server, the server returns a response and when the response arrives in the client the connection with the server is over. Even in single-page applications the logic is this, only the server happens to serve a Restful API.

Because I’m used to this approach I’m not being able to see what are the utilities, the real use cases of a persistent connection. This seems to be a very useful thing, but in which cases is it really relevant and makes sense to use?

I always see the same example of chat, which has several connected clients and they need to receive messages from others in real time. All right, it’s a valid example, but the only case of using this kind of technology is sending messages from the server to the client without depending on the client requisition?

2 answers

2


Think of a website where people post questions and answers, comment, and do other things. Think that people will want to know when there is a change in the list of questions, when someone changes something of their interest, communicates directly with the person, or when someone votes and their reputation changes, when there is a moderation queue to review.

This is an example of website that would benefit from a persistent connection.

But don’t cling to a concrete example. Understand why persistent connection is useful.

When you use REST the application is explicitly requesting server information. The user in one way or another determined what and when will ask something to the server.

Websockets are useful when you want the opposite, want the server to communicate with the client. Let him determine what and when to send some information to the customer. What is:

  • chat
  • networks of interaction between people (social, technical or other)
  • games
  • various monitoring (equipment, financial market, feed of various news, etc.)
  • collaboration between work or study team members
  • real-time monitoring of the usage flow of website by users
  • update based on external data (GPS for example), etc.

In addition, communication can be more efficient if used appropriately. This way you traffic only what is really necessary, only the data that has changed.

One last reason I can remember is when you’re interacting with something that only provides this API.

It is interesting to have a mechanism of ping to close a connection by the server to not keep connections open when they are not active. It is common for the client not to close the connection. It has several techniques to make the forced closure but it is already another subject.

1

This is exactly the same point that Client-Server developers, accustomed to permanent connections, raised when dynamic web applications began to popularize themselves using frameworks such as CGI, Webbase, ASP and similar.

'We can do everything with a permanent connection. What is the advantage of adding all this new overhead?'

The interesting thing is that many of the points defended in the distant past can be used to justify the use of Signalr and similar:

  • You save the process of all the handshaking needed to establish a connection;
    • especially important if you are running under HTTPS/TLS/SSL (which is why several secure applications request Keep-Alive);
    • Least handshaking means shorter response time;
    • Smaller amount of data trafficked;
    • More responsive application.

In short, persistent connections benefit any application that requires a large amount of data to maintain its transactional state or that needs to minimize server-client latency.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.