Rest is Sync or Async? / How WebSocket works?

Let begin with this what is HTTP? It is a Hypertext transfer protocol everyone know about that. Before talking about HTTP we must be aware of the OSI model.

What is the OSI model?

The Open Systems Interconnection (OSI) model describes seven layers that computer systems use to communicate over a network. It was the first standard model for network communications, adopted by all major computer and telecommunication companies in the early 1980s. The modern Internet is not based on OSI, but a simpler TCP/IP model.

Let see the OSI model with an example:

Think we have two people. They are A and B, A wants to send a package to B. A need some physical way (physical layer) to reach his place (Like a road). Then we need a mechanism to send this package, A to B using this physical layer.

The sender must know the address of the receiver otherwise sender can’t send the package to the receiver. Then we need somebody to transport this package using this vehicle on this physical path to that address. When sending the package we must need to track the package. Like what happened to the package where the package is now, like that we need to track the package. (We need to have a mechanism to connect the destination and the package). Then after delivery, the package receiver can access the package and he/she can look at what the package looks like is.

  • Physical layer: — Like our road, we should have a physical layer to send this package. The physical layer doesn’t enough unless you don’t have a way to send something from this end to other ends. The physical layer is responsible for the physical cable or wireless connection between network nodes. It defines the connector, the electrical cable or wireless technology connecting the devices, and is responsible for the transmission of the raw data, which is simply a series of 0s and 1s while taking care of bit rate control.
  • Datalink layer: -How this package has been transported. (Whether it is a car, bus, or lorry) like that what is the mode of transportation? (Switches: — it’s switching through the logical structure). The data link layer establishes and terminates a connection between two physically-connected nodes on a network. It breaks up packets into frames and sends them from source to destination. This layer is composed of two parts — Logical Link Control (LLC), which identifies network protocols, performs error checking and synchronizes frames, and Media Access Control (MAC) which uses MAC addresses to connect devices and define permissions to transmit and receive data.
  • Network Layer: — Address of the receiver. / It has the IP address. (Hub) In the network layer we can do whatever we can give block or give access control in the network level itself. We can block the IP address we can block the subnets. We can route to the subnet. The network layer has two main functions. One is breaking up segments into network packets, and reassembling the packets on the receiving end. The other is routing packets by discovering the best path across a physical network. The network layer uses network addresses (typically Internet Protocol addresses) to route packets to a destination node.
  • Transport Layer: — It transports this end to the other end. Like TCP or UDP. TCP grantee about the delivery but UPD doesn’t grantee about the delivery. The transport layer manages the delivery and error checking of data packets. It regulates the size, sequencing, and ultimately the transfer of data between systems and hosts. One of the most common examples of the transport layer is TCP or the Transmission Control Protocol.
  • Session Layer: — we need to check the state of the package by using the session we can do that (API). The session layer controls the conversations between different computers. A session or connection between machines is set up, managed, and terminated at layer 5. Session layer services also include authentication and reconnections.
  • Presentation Layer: — When he gets the package how does he see it. (Like mp4, mp3 like that). The presentation layer prepares data for the application layer. It defines how two devices should encode, encrypt, and compress data so it is received correctly on the other end. The presentation layer takes any data transmitted by the application layer and prepares it for transmission over the session layer.
  • Application layer: — This layer has the HTTP belong. (HTTP, DNS, ssh). At this layer, both the end-user and the application layer interact directly with the software application. This layer sees network services provided to end-user applications such as a web browser or Office 365. The application layer identifies communication partners, resource availability, and synchronizes communication.

Why HTTP is important?

Proving rest is not Asynchronous

  • Asynchronous: — You go with the resource and do other works until resources finish their work (rest).
  • Synchronous: — is you went with the resource and spend the time until the job gets done (Soap).

The only client can call the server. In HTTP there is no permeant caller receiver. Whoever made the calling client whoever received the call receiver. The server is nothing but receiving the call. Technically server must have an address.

There are two versions of that request.

  • HTTP 1.0
  • HTTP 1.1

Http 1.0 -> open the connection / send the message / get the response (close the connection)

Http 1.1 -> open the connection / send the message / get the response (This is called keep alive)

Here HTTP 1.1 is faster than HTTP 1.0 because the connection is kept alive.

If the server wants to end the connection sever send the TCP (fin) header to the client. Fin header means: — I want to close the connection when the server sends the message to the client. After that server didn’t accept any message, the server tells I’m going to talk to you but I’m not going to listen to you anymore. This means half-duplex. Sever don’t reply to the new message and the client can only send the message but the server doesn’t reply to the new messages. (The server send the message connection is closed) .If we want to send a new message we need to create a new connection.

Why HTTP introduce HTTP 1.1 instated of HTTP 2.0 because they only change the connection don’t close anymore (You can’t change major functionality the Because of the backward compatibility).

  • In 1.0 you know the response belongs to which message (Because you send the message and you get the response)
  • In 1.1 when we send the four messages to the server, the server should respond to the message in whatever order the server takes the request. ( HTTP 1.1 has a role client can send any number of requests to the server but the server must respond to that request according to the request server get request)

According to that, we can tell HTTP is in synchronous mode. As we used the HTTP protocol for the rest, the rest become synchronous. Because the HTTP protocol is synchronous.

To avoid the synchronous system you can use the Java messing brokering system. whenever you need the request you send it to the message brokering system another side is listening to it and taking it and processing it and giving it back to the message broker and you take it and process it. (that’s become asynchronous).

The request has two parts

· Header

· Body

There is a naming convention called status code it tells what happen to the client. (Request do not have status code response have the status code)

Status code mainly have five serious

  • 100 serious:- Information Messages
  • 200 serious:- Success
  • 300 serious:- Redirection
  • 400 serious:- Client-Side Error
  • 500 serious:- Server Side Error

Headers let the client and the server pass additional information with an HTTP request or response. When you send a request you can tell the content type (what is in the body).


In the HTTP only one person can talk. Whoever is talking become a client whoever receives become a server. Only one person can talk. I think the server wants to send something or tell something to the clients how do they tell? Because the client don t have a real IP address.

For Example, Facebook has a fixed IP address but we don’t have a fixed IP address because the router from time to time changes the IP address. Then Facebook cannot talk to you. You send the request to Facebook Facebook only can respond to you. Then we need the way to mechanism to send the request from the server. For that, we can use the WebSocket.

WebSocket is nothing but HTTP. But it is breaking the barrier between the client and the server. People have miss understood Websoket as protocol. But Websoket is running on HTTP.

Example: — we send API requests to the server and make the connection. And the same time asks he can update the protocol using the upgrade header. The header is upgrade value is WebSocket.Then server and client has an open connection. It is a full-duplex connection.

When I send the request server hold the tunnel that means if I want to send the request server to the client you can put it to the tunnel and go and hit the server. We can use this thing in unlimited use cases.

  1. Origin — the origin of the client page, WebSocket objects are cross-origin by nature. There are no special headers or other limitations. Old servers are unable to handle WebSocket anyway, so there are no compatibility issues. But the Origin header is important, as it allows the server to decide whether or not to talk WebSocket with this website.
  2. Connection: Upgrade — signals that the client would like to change the protocol.
  3. Upgrade: WebSocket — the requested protocol is “WebSocket”.
  4. Sec-WebSocket-Key — a random browser-generated key for security.
  5. Sec-WebSocket-Version — WebSocket protocol version, 13 is the current one.

How does WebSockets Work?

Let’s go through these simple steps:-

  1. The client sends a request to the server.
  2. A connection is established.
  3. The server sends back the response.
  4. The client receives the response.
  5. The connection is closed.

These are the steps involved in establishing a WebSocket connection.

  1. The client (browser) sends an HTTP request to the Server.
  2. A connection is established via the HTTP protocol.
  3. If the server supports the WebSocket protocol, it agrees to upgrade the connection. This is called a handshake.
  4. Now that the handshake is complete the initial HTTP connection is replaced by a WebSocket connection that uses the same underlying TCP/IP protocol.
  5. At this point, data can flow back and forth freely between the Client and Server.

Thank you!




Associate Software Engineer. |

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

UnitHow to Create a Collectable in Unity

Implement Effective IT Asset Duplicate Management With AssetSonar

IT asset duplicate management with AssetSonar

Format function in Python

Building A Bridge Hand Generator With Python Part One

A testing kit for covid 19

How to Connect Scrapy to your PostgreSQL database

How the programming community changed in 1 year

Crodo VS StarTerra: Battle Of Launchpads

the neurological symptoms Covid-19 patients experience Jacobson told me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nisal Jayathilaka

Nisal Jayathilaka

Associate Software Engineer. |

More from Medium

SpringBoot: API Authentication Using OAuth2 With Google

A Fun Approach to Strategy Pattern

Setting up Keycloak and Securing Spring Boot Rest APIs

Spring Cloud Gateway — Securing Services with API Key