Ramin Ahmadi | Personal website of Ramin Ahmadi

Web sockets and how they work

Foundation of web as we all know is based on Hypertext Transfer Protocol (HTTP) which uses request-response protocol. This, in a nutshell, means a web browser submits an HTTP request message to server and server which, holds resources, returns appropriate response message. However, servers weren’t allowed to send messages to browser because they couldn’t initiate a request. But many applications relied on real-time resources being sent to client from server such as messaging systems. It would be very annoying to ask user to click on a button so a request can be sent to server and latest data being received. Of course, writing an application that constantly send requests only creates overhead on network traffic and it is not efficient at all.

Hence, there was a need for a persistent connection between browser and server whereby both parties can send and receive data. Web sockets are solutions to maintain bidirectional communications with servers. But how do we make web sockets?

I’ve create a simple twitter app that sends and receives data to server which includes a html page that has a textbox, button and placeholder to present status of server responses.

You can download the Gist from my code repo or directly from Jsbin.

Leave a Comment.