What are Websockets?
Published: 2023-01-18 02:23:25

## What are `Websockets`

`Websocket` is a communication protocol between client and server.

It establishes a two-way communication protocol (full duplex communication) and keeps a persistent connection between client & server.

Websockets allows near real-time communication between client and browser

We can send data in text or binary with `websockets`.

## HTTP VS Websockets

With HTTP 1.1, we have the “keep alive” header. So only initial request from a client requires a TCP handshake. Subsequently all client requests just gets a response back from the server.

But it still requires the client to send a request to the server over the HTTP protocol and wait for a response from the server. Server just waits to be asked for resources.

`Websockets` also uses HTTP and TCP/IP. HTTP for initial request where it sends a connection upgrade request to the server (to upgrade the protocol from HTTP to web socket)
TCP is also involved but same thing only on initial request from client.

So the difference between HTTP and `websockets` is that websockets persists its connection and allows bidirectional communication between servers and clients where typical HTTP does not.

HTTP is request/response meaning that regular HTTP protocols requires the client to send a request in order for a response.
`Websocket` allows duplex communication so that means the server doesn’t need to wait for a request before fires a response.

## Alternatives to websockets

- Event Source / Server Sent Events
    
    This is quite similar to websockets but is mono-directional meaning the server can only push data to the client. something like a webhook in the client side
    
- Polling
    
    There are generally two different types of polling, Short & Long polling.
    
    `Short polling`  sends a single request to the server at an interval regardless of whether the server has ‘new data’ whereas `Long polling` sends a single request to the server and waits for ‘new data’ to be available.
    
    In short polling, we may expect to receive an empty response from the server when there are no new updates. This could have an impact server load depending on the interval time the client sends a request to the server. We are generally wasting resources this way as the server has to parse request headers, query for new updates and handle the response object.
    
    Long polling on the other hand sends a request and waits for updates on the server side. Meaning we only send one request each time. But this still has its overhead as we still need to establish the TCP Handshake each time we send a request. This can get quite expensive if done in regular intervals in terms of server resource usage.
    
All these solutions comes with an overhead and needs proper planning and determining its trade-offs before implementing.
    

## Why use websocket

`Websocket` allows near real time updates, it is fast by design (keeps a single persistent connection) and doesn’t require constant request/respond cycles. 

This is especially useful when developing a real-time chat application or when you need your client to react as quickly as possible to events that happen within the architecture