WebSockets

If we want to make an application in real time we have two alternatives:

  • use HTTP
  • Use WebSockets

Problems using HTTP for realtime applications

Nothing prevents us from using HTTP to be able to create our application in real time, such as a chat or a fleet location system. However, in HTTP we would have to carry out not very efficient processes.

If, for example, we are developing a chat, the client would have no problem sending messages. Those messages would be sent to the server and that’s it. However, to find out that there are new messages in the room, other people would have to initiate an HTTP request, since HTTP only allows connections to be started on the orders of clients.

Thus, if we have 100 people in a chat room, all these people would have to be constantly, for example, every 5 seconds, making a request to the server to find out if there are new messages. This would be 100 requests every 5 seconds, 1200 requests per minute. If we think of multiple chat rooms, we would possibly have a saturated server quickly.

In addition, with each request from a client, the HTTP protocol requires a whole ceremony to be able to transfer information, even if the response is simply something like “no new messages”, the server requires to initiate the request, and perform various communications to send the response. , after a few seconds.

How WebSockets enables the improvement of realtime applications

On the contrary, with WebSockets the connections remain open. This means that there is no kind of ceremony to send the data, they simply travel through the open channel, which is infinitely faster.

See also  News on the Internet

But the most important, the server can initiate communications. This means that when a message is received, the server will transmit it on the open channel for all the people in the room, without the clients having to do anything. This avoids unnecessary connections to the server, simply for the server to respond “nothing new”.

Thus, WebSockets allow for more efficient communications, as they are faster and do not require unnecessary connections from the client to the server to request data. The server will simply send them when it has them, period.

Loading Facebook Comments ...
Loading Disqus Comments ...