WebSocket protocol

Abdullah
3 min readJul 4, 2023

--

Hi there, I will talk about the WebSocket and how it works, the use cases, and the differences between the ws and the HTTP.

What is the WebSocke?

WebSocket is a duplex protocol used mainly in the client-server communication channel, and WebSocket needs support from HTTP to initiate the connection.

Why is a Web Socket Needed and When Should it be avoided?

Use WebSocket When You Are:

1-Developing real-time web application

The most customary use of WebSocket is in real-time application development wherein it assists in a continual display of data at the client end, the use of WebSockets makes such data transmission quick and leverages the application’s performance.

A real-life example of such WebSocket:

  1. Bitcoin trading website

2-Creating a chat application

3-Working up on gaming application

WebSocket shouldn’t be taken on board when old data fetching is the need of the hour or need data only for one-time processing. In these cases, using HTTP protocols is a wise choice.

WebSocket vs HTTP

People often get confused and find it difficult to pick one out of these two:

But as told previously, WebSocket is a framed and bidirectional protocol. On the contrary, to this, HTTP is a unidirectional protocol functioning above the TCP protocol. HTTP is stateless and is used for the development of RESTful and SOAP applications.

In WebSocket, communication occurs at both ends, which makes it a faster protocol. In HTTP, the connection is built at one end, making it a bit sluggish than WebSocket.

WebSocket uses a unified TCP connection and needs one party to terminate the connection. Until it happens, the connection remains active. HTTP needs to build a distinct connection for separate requests. Once the request is completed, the connection breaks automatically.

How are WebSocket connections established?

The process starts with a WebSocket handshake that involves using a new scheme ws or wss. The ws dosen’t use TLS but the wss use TLS likes the http and the https.

Using this scheme (ws or wss), servers and clients are expected to follow the standard WebSocket connection protocol. The WebSocket connection establishment begins with HTTP request upgrading that features a couple of headers such as Connection: Upgrade, Upgrade: WebSocket, Sec-WebSocket- Key, and so on.

Here is how this connection is established:

1-The Request

  1. Connection: Upgrade header denotes the WebSocket handshake while the.
  2. Sec-WebSocket-Key the server has to prove to the client that it received the client’s WebSocket handshake, so that the server doesn’t accept
    connections that are not WebSocket connections. This prevents an
    attacker from tricking a WebSocket server by sending it carefully
    crafted packets using XMLHttpRequest [XMLHttpRequest] or a form
    submission.
  3. Sec-WebSocket-Version can explain the WebSocket protocol version ready to use for the client.
GET ws://websocketexample.com:8181/ HTTP/1.1
Host: localhost:8181
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: b6gjhT32u488lpuRwKaOWs==

2-The Response

  1. Sec-WebSocket-Accept has the sec-websoket-key in hash value
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: rG8wsswmHTJ85lJgAE3M5RTmcCE=

Conclusion

WebSocket enables two-way interactive communication sessions between client and server without having to poll the server for a reply, providing both speed improvements and real-capabilities over other protocols. But as with all applications, using WebSocket entails both careful programming practices and run-time protection to guard against a unique set of threats. This API defense-in-depth strategy will improve protection for both your users and your organization compared to traditional approaches.

twitter: https://twitter.com/0xmicro

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Abdullah
Abdullah

Written by Abdullah

Bug Hunter, web pen tester

No responses yet

Write a response