Polling vs WebSockets vs Socket.IO

Tre'von Mitchell
5 min readOct 26, 2021

--

Introduction.

In this article, we will go over the differences between Polling, WebSockets, and Socket.io and how each one of them works. I just started to work with Socket.IO on a recent project for the first time. It was new and challenging, and I am still learning more about Socket.IO. Socket.IO is pretty cool once you can see how it works and how you can develop a real-time chat experience with your web application.

HTTP Request/Response

I am sure by now you know how most of the internet works over HTTP request/response model. In the process, the client sends the request to the server, and based on the request the server sends back to the response. In a simple example, when we click on a link and request the server to show us a page, the server will respond with the contents of the page. The client has to initiate a request before the server sends back a response. The server is just sitting there constantly listening for any requests, and the server cannot independently push data to the client without the client having requested it at first. While the model works fine, if you’re requesting data that is already in the server, or if you want to view a web page and the page is already there in the server, then the server can send us the page the moment we request it. This model is not going to work well for a chat application. Do you know why?

When a server receives a new message, that our friend or user sends us, it cannot proactively push this message to us because in a typical HTTP client-server model the client has to request data before the server can send it. The server has to patiently sit on this new data until the time the client asks the server to send this new data. How can we get around this?

HTTP Short Polling.

In Short Polling, the client will automatically check with the server for new data at predefined intervals. The server still cannot send a response without a request from a client, however since the client automatically places the request periodically the new data is also sent to the client at regular intervals. Think of it as a kid in the car who after every couple of seconds will ask “are we there at?, are we there at?”. Depending on how short is the time gap between each automatic request that the client sends short polling can feel like real-time.

Long Polling.

Long Polling is similar to short polling expect the client does not blindly keep checking for new information at a set interval instead in this model the client makes the request, the server holds on to the request until new data is available only when the new data is available will the server send data to the client. Once the client receives the new data, it instantly pulls the server for new data again. The server once again, holds on to this request until any new data is available and the moment new data is available it sends it to the client right away. This seem like a viable option for a chat application.

WebSockets.

After the initial handshake, both the client and the server established a permanent connection through which they can simultaneously send and receive messages to and from each other. In WebSockets protocol, the server does not have to wait for a request from the client to send any new data. Once the server receives any data, it sends it automatically to the client. This way is bi-directional and is different from the request-response model. The connection of this is permanent until either the client or server chooses to close it. How will this work for your chat application? Well, we should not just send messages to ourselves we have a few other friends connected to the server. Once we type in a message and press send, the server can proactively dispatch the message to all the clients connected without those clients having to pull the server for new data. When one user sends a message to the group, the server can push the message to all the connected clients proactively without each client having to ask the server to send new data. This real-time information exchange is what makes the chat application

Socket.IO

Socket IO is a JavaScript library that enables real-time web applications. Socket Io allows clients and servers to have bi-directional communication. How is this different from WebSockets? WebSockets is a protocol like HTTP, it's a piece of technology. Socket IO is a JavaScript library and it will upgrade a connection between a client and a server to WebSocket protocol. If this is possible, it downgrades the connection transparently to the HTTP polling method. Socket IO will do all of this in the background for us. As a user, we don’t have to worry about switching from one protocol to another. The user experience will seem like real-time communication.

I hope this article became useful for you and that you will learn more about Socket IO or WebSocket and use real-time communication for your web application.

--

--

Responses (1)