Dylan Rogers
4 min readApr 4, 2021

Human beings love to send and receive messages, and as technology has progressed a major focus has always been improving how we communicate. Homing pigeons, the Pony Express, letters, the telegraph, SMS, email; and now receiving high-quality images and data from other planets. WebSockets are large improvement on sending/receiving information online for real-time communication. With just HTTP there are issues around the lag between a user issuing a request and later receiving a response from a server.

Once upon a time people would play chess by post. I would make a move, write it down in a letter (maybe send a friendly hello as well), send the letter in the post, it was picked up by a mail carrier, delivered to the post office, sent out with another mail carrier and then delivered to my friend. Then, I have to wait for my friend make their move and do the same thing.

A postcard used for chess by post. From Wikipedia.

WebSockets instead are like playing with a friend over a video chat, we can see each other’s board and keep up-to-date with our moves without having to be in the same room. They allow for “two-way interactive communication session between the user’s browser and a server” as per MDN.

A user will access a server by using an HTTP connection, then if the client’s browser and the server are able (almost all browsers are WebSocket compatible now) they switch to a WebSocket protocol while still using a TCP (Transmission Control Protocol) connection. A WebSocket connection stays open sending information back-and-forth in data packets so that each side is aware of what the other is doing. This is incredibly useful for messaging apps or online multiplayer games!

WebSockets have now been around for a decade and have been adopted widely across many platforms.

Libraries for WebSockets are available for many programming languages. The browser takes care of the client-side connection and the server-side connection will be run by a compatible library.
Examples include:

At this point in time the use of WebSockets is so ubiquitous that Action Cable is actually built into Rails. Action Cable’s merge into Rails means that you can use all of your same familiar Rails terminology and programming style. Using Action Cable a server can have multiple two-way connections active at a time.

Example of an ActionCable connection class in Rails.

Setting up and using WebSockets may seem complicated, you have to run your own servers and develop the methods of communication between your servers and your users, if that isn’t something a company wants to invest in there are other options.

Communication API solutions are operated and managed by many different companies, two large ones include Pusher and Twilio. They can provide scalable infrastructures so even a small start-up can make use of them or a huge media communications company will use their services.

With one of these companies you no longer need to worry about operating your own servers or setting up the minutiae of how you want to communicate with your clients. Instead you choose from the multitude of options they have available (or follow along with their handy tutorials to get how you can use their services) they have many programming language options and frameworks dedicated to the platform of your users, whether that is via web or mobile.

WebSockets infographic from Ably.com

WebSockets have allowed for the creation of a dynamic and active internet. Real-time communication and notifications, location tracking apps, collaborative engagement, and more are all possible through the development and implementation of this protocol. A major improvement in the quality of interaction on the web.

Sources:

https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API