WebSockets vs HTTP - What WebSockets are and how they differ from HTTP connections
![WebSockets vs HTTP - What WebSockets are and how they differ from HTTP connections](/content/images/size/w2000/2023/01/websockets-vs-http.webp)
WebSocket API
WebSockets are a communication protocol that enables real-time, bidirectional communication between a client and server, using a single TCP connection. Unlike traditional HTTP connections, which rely on the request-response communication model, WebSockets establish a persistent connection between the client and server.
WebSockets are mainly used in web applications that require real-time updates and can benefit from this bidirectional communication, like a chat application or an online game.
![Diagram: WebSocket - how it works](https://tutorialsight.com/content/images/2023/01/websocket.webp)
What is REST?
REST (Representational State Transfer) is an architectural style for building web services that uses the HTTP protocol for communication. RESTful web services have a uniform interface, are stateless and cacheable, and provide endpoints for clients to interact with using standard HTTP methods like GET, POST, PUT and DELETE. They are widely used in web and mobile applications, and are simple to understand, implement and consume.
![Diagram: Rest - how it works](https://tutorialsight.com/content/images/2023/01/rest.webp)
How much faster is WebSocket than HTTP?
WebSockets are faster than HTTP because they establish a persistent connection between the client and the web server. By doing that there's no need to open a new TCP connection to request new data. WebSockets are much faster than the traditional request-response model of HTTP because they remove the latency that new TCP connections creates when requesting new data.
For example, imagine you want to build an online game where it's fundamental to see all opponent's interactions in a map. With WebSocket, the connection is persistent, and you immediately see when the opponent moves on the map. On the other hand, if you don't use WebSocket, you've to constantly query the server by creating new HTTP requests, asking where's the opponent. By the time you receive back the information from the server, the opponent could be in a different position on the map.
Another example where you might need WebSocket, is when trying to build an online real-time collaboration platform.
Are WebSockets better than HTTP?
WebSockets and the HTTP protocol are both used for client-server communication, but they have different purposes and characteristics. The HTTP protocol, which uses a request-response communication model, is well-suited for transferring data between a client and a server. However, as we said earlier, it's not well-suited for real-time applications.
The WebSockets protocol, instead, utilises a persistent connection, that is designed for low-latency communication and real-time interactions, making it a better option for certain types of web services and applications.
As you might start to notice, WebSocket is not better than HTTP and HTTP is not better than WebSocket. You have some advantages when you use WebSocket compared to HTTP in specific situations. That's why, when deciding between using HTTP or a WebSocket, it is important to consider the specific requirements of your application.
For example, in some situations you might need some of the benefits that WebSocket gives you for a rarely used feature of your application. If the amount of work required to implement WebSockets in your application is not justified, than you might going to consider the long polling technique using HTTP requests.
Stateful protocol - An example of a stateful WebSocket connection in a Chat application
A common example of a stateful WebSocket connection is a chat application. In this scenario, a client establishes a connection with a server using the WebSocket protocol, and can send and receive messages in real-time. The WebSocket server keeps track of the connections that have been established, and it maintains the state of each connection.
For example, when a user connects to the chat application, the server assigns them a unique identifier and adds them to a list of connected users. As messages are sent and received, the server updates the state of the chat room and pushes those updates to all connected clients. When a user disconnects from the chat, the server removes them from the list of connected users and updates the state of the chat room accordingly.
This example illustrates how the WebSocket protocol can maintain a stateful connection between a client and server, allowing for real-time updates and interactions. This is different from stateless protocols, where a new request is needed for each new piece of data, or for each new connection, which makes it less efficient for real-time interactions.
Stateless protocol - An example of a stateless communication using a REST API
A common example of a stateless communication using a stateless protocol like REST APIs and the HTTP protocol is a weather application. In this scenario, the client sends an HTTP request (GET) to the server, asking for the current weather in a specific location. The server, using a RESTful web service, processes the request, retrieves the relevant data from a weather database, and sends back an HTTP response with the requested information.
The client can then process the response and display the current weather information to the user. The server does not keep any information about the client's past requests or the current state of the client, and the client does not need to maintain any information about the server's state. Each request is treated independently and is stateless, meaning that the server does not need to maintain any knowledge of the client's previous requests and the client doesn’t need to maintain any information about the server's state.
This example illustrates how the HTTP protocol and REST APIs can work together to provide a simple and consistent way to access resources over the web.
WebSocket protocol and SSL
The WebSocket protocol, like HTTP, runs directly on TCP. However, when it comes to secure connections, WebSocket Secure (WSS) establishes an encrypted WebSocket connection using the same technology as HTTPS. The underlying layer of HTTPS is TLS/SSL, which encrypts the data sent over the connection, and it runs on TCP. Similarly, WSS also runs on TLS/SSL, providing an encrypted connection, which is layered on top of the standard TCP connection used by WebSockets. To put it simply, WSS is the secure version of WebSockets, just like how HTTPS is the secure version of HTTP, allowing for encrypted communication over the WebSocket connection.
Conclusion
We now know the key differences between the two protocols and we should have a understanding on when WebSockets should be used instead of HTTP REST APIs.
We'll cover some of these concept in a following tutorial on how to create a Chat Application using JavaScript, WebSocket API and a server written in NodeJS.