Have you ever come across the need of establishing a 2-way connection between the client and the server? If yes, then here is the solution !๐
Introduction to Web Sockets? ๐ค
Web Sockets are protocols that help in creating 2-way communication sessions between the client and the server. It acts as a duplex connection over the single TCP network.
Still Confused right ?๐ญ Let me give you an example before diving into the technical side of this.
Imagine a situation when you are on a phone call with your dear friend. In this communicating session, both of you are talking and at the same time listening to each other. This is exactly what 2-way communication means.
Technically, this helps create a stable connection between the server and the client, and the changes are auto-updated on the frontend side whenever there are some changes on the backend (server).
How this session is created?
When a single TCP Connection is established, the connection is being initialized with HTTP protocol which later gets upgraded to form a WebSocket connection.
Difference between Stateless and Stateful Protocols:
HTTP is a stateless protocol, unlike WebSocket which is a Stateful protocol.
Stateless :
- Server doesn't need to know about the state.
- Crashes are handled smoothly.
- Server Design is simple.
Stateful :
- Server needs to know about the state i.e a server is created to store the session info.
- Crashes are not handled (Does not have success listeners)
- Server design is complicated.
Pros and Cons of using Web Sockets ๐งโ๐ป:
Pros :
- Using a WebSocket on your project is easy, you just need to make a handshake between the server and the client.
- Real-time performance is very smooth.
- WebSockets are stateful protocols unlike HTTP.
- Provides better Binary Support (Binary files are the low-level files that are interpreted by a program)
- The data header for WebSockets is relatively small.
Cons :
- Less Scalable i.e in some situations when the client is sending a large amount of data continuously, a single server feels very loaded.
- WebSockets require an initial HTTP connection which upgrades into a WebSocket.
- Edge caching is not possible with WebSockets, unlike HTTP.
Note: Everything has its advantages and disadvantages which depend upon your use case. If your use case demands the use of Web Sockets then you have to implement it irrespective of the disadvantages which may be handled later.
Some References for implementation guide: ๐คฉ
Thanks for investing time in reading this blog. Hope you got to know something new and interesting. Leave a like if you found it valuable and comment if you have any doubt/feedback. Have a good day :)