top of page

What are WebSockets? Everything you need to know about WebSockets! (2023)

Updated: Dec 9, 2022

If you are someone who wants to explore WebSockets and know about how they work, or if you are planning to use WebSockets in your project and want to learn about its implementation and code WebSockets, this blog is for you. If you want your website to update data instantaneously in realtime or you want to display server-triggered notifications/messages on your website, you need to use WebSockets.

Before diving straight into Websockets, I will give a bit of an explanation of how HTTP/HTTPS protocol works. This will help to understand why WebSockets are required and what problem it solves.

Let's dive right into WebSockets -

  1. How does a HTTP/HTTPS protocol work?

  2. What are Websockets?

  3. How do WebSockets work?

  4. WebSockets Handshake

  5. Where are Websockets used?

  6. When not to use WebSockets?

  7. WebSockets - Pros and Cons

  8. Coding Websockets

  9. Server-side coding

  10. Client-Side Coding

  11. Websockets implementation examples on the internet

  12. My thoughts and Conclusion

  13. Frequently Asked Questions (FAQs)

1. How does a HTTP/HTTPS protocol work?

The HTTP protocol is based on a request/response system, i.e. the client (user) computer always makes a request and the server sends the response. The point to be noted here is the client/user will always be the first to initiate the request. The server won't randomly send a response to the user. Most of the public transactions on the internet happen using this HTTP protocol.

For example, whenever I open a website, let's say I open - My computer will send a request to a backend server via the internet. This request is sent using the HTTPS protocol. Whenever this request is sent to the web server, a response will be sent back from the server to my computer. To send this request a TCP connection is first established between my computer and the server. When the response is received, this TCP connection is closed.

Every time I open a URL or my web app calls a REST API, a TCP connection is opened. Once the response is received, the TCP connection gets closed. A certain amount of time is consumed in creating this TCP Connection.

In short, if my web app calls 20 different API calls to a web server, 20 different TCP connections will be created. Each of them will be closed once the responses for them are received. This is how an HTTP/HTTPS connection works.

Imagine the amount of extra time consumed by creating each of these TCP connections! Opening 20 TCP connections is very time consuming and increases latency of the web application.

HTTP protocol can be defined as a uni-directional protocol that creates a connection using the HTTP request method. The connection is closed once the response is received.

In HTTP Protocol, the client/user tells the server - "Hey give me data", but there can be use cases when the server wants to send a message to the client/user without the client requesting it. This is not possible us