Real Time Communication with Sockets

Akshit Grover
codeburst
Published in
5 min readNov 30, 2017

--

Today most of the applications which delivers information are driven by Real time communications, ever used what app? Whatsapp’s unique selling point is Real time communication which in turn uses sockets.

what do we mean by real time communication?

Well it is no different than conversing to a person physically but here you use a network and device to connect to it, so that you can use the bliss of sockets. Sockets are the key to drive real time communication for almost 98% of the applications.

what are sockets?

Keeping the technicality aside, Analogy: A Postman delivers you the post by himself, you don’t have to check if there is a post with your name on it, if there is any post with your name, it is delivered to you by a postman you don’t have to request him to deliver it you.

Think of sockets like a postman, who delivers you the content with your name on it over the web, you don’t have to constantly request to check if you have any message or email, sockets does it all for you.

Take an example of social giant Facebook:
Do you refresh your page every time you want to see a message? No right, You get notified if there is any message or any activity with your account, guess what, socket is the architecture behind it.

So What’s Next……

Before Getting into sockets, It would be more appropriate to discuss the HTTP first and how sockets evolved and what was the need of sockets……..

What is HTTP?

HTTP the hyper text transfer protocol is a stateless which is used to transfer information over the web. Protocol has various standards which every information exchange transaction follow.

What do they mean when they say HTTP is stateless……?

HTTP is said to be the stateless protocol because every request which made to receive or send data is independent that every request is unique and no data is used from the previous request by the HTTP to make a new request, which is what makes HTTP powerful.

BUT, a server to respond to the request it needs certain information about the client who is making the request or what is the content of the data or what type of data is delivered.

How a server gets this information?

Pretty obvious solution is :

For every new connection a client can make a request to the server about the meta data of the connection which will state, content , content-type, origin of the client and a server getting the new connection can save the data for the client and can respond using the meta data provided over the new connection established. But this will make the client-server communication state full and in turn result in unnecessary LOAD on the server as it will have to store information about every new connection established.

So how to remove this unnecessary load over the server?

Yea, The answer is HTTP.

Now, The question arises how HTTP does this?

For every request a client makes using any software like a web browser HTTP attaches some headers which provides meta data to the server to serve the request. So, this makes each and every request a client makes independent, Though the server can use sessions and cookies for efficient performance.

Following is an image showing some of the headers attached to a HTTP request:

How Real time communication over the web evolved?

There are many ways developers have used to establish the essence of real time communication over the web including polling, long polling and sockets.

What is…….

POLLING:

Polling is a way in which a client keeps on making request to the server and gets a response if some message is there, which is actually a pretty good way but what is there is no message still a client is making request with all those headers of HTTP request. This not only increase the bandwidth over the channel but also increases the LOAD on the server to handle sluggish requests.

Long POLLING:

Long polling is an upgraded version of polling in which once a connection is established between a client and server it is saved unless it is for client to log off. For this to happen a server has to store the connection locally so that it can do the processing and send the response to client when some message arrives then after client makes a new requests and the cycle goes on. This Solves the problem of unnecessary requests but still server has to spend extra computation and space to keep the connection alive. Better but not ideal !

To avoid all these issues with the current technology sockets were developed.

What are sockets?

Sockets are a protocol which is given as a proxy for HTTP protocol. Sockets are considered as ideal mechanism for real time communication as it reduces the overhead time which is required to process the meta data by the server in order to respond to a request, sockets also decreases the latency over the transmission channel.

How all of that is done…………..?

Socket has it’s own protocol ws:// and wss:// web socket protocol and web socket secure protocol. Since it is given as a proxy over HTTP it also uses port 80 and 443 as used by HTTP.

This web socket proxy is applied over the HTTP during the initial handshake between the client and the server.

Initial handshake takes place when a client request the server for real time service, This initial request contains all the headers required by the server to process the request. Once the connection is established web socket protocol is mapped over the HTTP to serve the further requests.

But the requests which takes place after the initial handshake contains a lot less headers then before. This reduces the latency and also the traffic over the channel.

Even with sockets a client has to send continuous requests to the server but the issue of high bandwidth is resolved as the amount of meta data send with the headers of the request is a lot less.

So this was all about the basics of sockets,
I recently published a npm module which makes the socket server over node and a lot more things to help one in Hack-a-thon.

npm: https://www.npmjs.com/package/hacken
github: https://www.github.com/akshitgrover/hacken

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

--

--