What is WebSocket?
Tags: http, websocket, tcp
Published: 2022-06-07 11:25:35

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HTTP (Hypertext Transfer Protocol) and WebSockets are both TCP/IP (Transmission Control Protocol/Internet Protocol) based connections that are used to communicate from a client to a server or from one server to another. They are both used for different purposes and have different use cases.

#####_What is HTTP protocol?_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HTTP is a unidirectional TCP connection protocol that is used to send and receive data. When a client sends a request to a server, the server sends an appropriate response back to the client. After sending the response, the connection is closed. Each time a new request is made, a new connection is established and after the response is sent, the connection is closed. This is repeated every time a new request is made. This is known as stateless protocol.

#####_What is WebSocket?_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unlike HTTP, a WebSocket connection is bidirectional, which means that the connection is open for both sending and receiving data. This is known as stateful protocol. WebSocket is a full duplex protocol, meaning that data can be sent and received at the same time. After closing the connection by either the client or the server, the connection is no longer open for data transmission. In contrast to HTTP, WebSocket starts with `ws://` or `wss://`.

![WebSocket vs HTTP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/md4oxyqp5kguqlmy0u32.png)
#####_When to use WebSocket_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;There are a number of use cases where WebSocket can be used in favor of HTTP. Let’s take a look at a few of them. WebSocket is a great way to implement real-time applications, such as chat, games, and real-time data transfer. 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A good example of one such real-time application is a stock ticker or stock trading website. A connection would need to remain open in order to send and receive data in real-time to get the latest price of a stock.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A gaming app is another good example of the need for real-time communication for seemingly obvious reasons. You wouldn't want to play a game that had to refresh the UI every time new information was sent or received. 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;And finally, the almost obvious example of real-time communication is a chat application. This is probably the most common use case for WebSocket. A chat application would need to remain open to send and receive messages in real-time.

#####_When NOT to use WebSocket_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebSocket is best suited when we need real-time updates or continuous connection between client and server. If we simply need to fetch old data and send new data then HTTP is the way to go. Older data which isn't updated on a regular basis can be fetched using a simple GET request. Newer data can be sent using a simple POST request. This is a good example of when to use HTTP instead of WebSocket.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I hope this article has helped you understand the difference between HTTP and WebSocket and when to use each one. If you are interested in learning more about HTTP and WebSocket, I suggest you check out some of the following resources:

[_Why is the WebSockets protocol better?_](https://stackoverflow.com/questions/14703627/websockets-protocol-vs-http/14711517#14711517)
[_WebSocket - MDN_](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
[_WebSocket - Wiki_](https://en.wikipedia.org/wiki/WebSocket)