WebSockets in English

WebSockets in English

A deep dive into real-time client-server communication.

·

3 min read

In the 21st century, due to advancements in computer technology, the world has become more interconnected like never before. The internet has opened more opportunities and innovations that seemed impossible. Companies like Amazon are now making billions in e-commerce online business.

Social media has become a necessity in almost every millennial's life; likes, comments, and other post engagements are now being delivered in real-time. It has become very common in many apps to deliver data to devices in real-time over the internet. This was not possible before, it's a dream come true.

Servers couldn't deliver data to online devices unless the device requested it. This changed when websockets were introduced in 2008.

So what it websocket?

What is WebSocket

The web was initially built on top of HTTP protocol which worked on the request-response model. The client initiates a connection and requests for something(e.g a web page), the server responds and immediately closes the connection.

Web sockets promise to keep the connection open so that the server can send the data to clients any time when it's available without waiting for the client to request it.

Wait, what is websocket?

I hear you, son.

A Websocket is a communication protocol on the internet that enables bidirectional communication between the client and the server. This means that the server and client can talk to each other at any time without the client having to initiate the connection. This is possible as soon as a web socket connection is established.

Before WebSockets came along, the “real-time” web existed, but it was difficult to achieve, typically slower, and was delivered by hacking existing web technologies that were not designed for real-time applications.

Life before WebSockets

Before real-time server-client communication, the web was built on top of the HTTP protocol which is a request-response-based model where the client had to request the data from the server. So applications like chat apps weren't possible due to this paradigm.

In a social network application, for example, if a user sent a post to the server, the post was just saved on a database and any user of the app can, later on, make a request to the server and get the latest posts from the database. Active users wouldn't get notified about the post instantly unless a user refreshed the browser.

Now with webSockets, any user who has established a websocket with the server can instantly get notified about the post. The server can implement the logic where users can chat privately with each other in real-time.

Applications of Web sockets

1. Chat applications

Humans are social beings and giving them a platform where they can connect with their family or friends simplifies and improves their lives. Notice how WhatsApp is popularly used among families and friends.

2. Online games

Lets two or more people enjoy a game together. For example, an online Chess game.

3. Logistics apps

Let users track their orders in real-time. You can keep track of the GPS location of the truck and send it to the user in real-time and display it on a map. This is also simply what Uber does, with some fancy algorithms.

4. IoT devices

You can connect devices on the internet and implement websockets so that the devices can communicate with each other. Smart homes have been on the rise lately.

The possibilities of websockets are only limited by your imagination.

That's it guys. That's my understanding about websockets, so let's go and connect the world!

Conclusion

Websockets are life-changing and have, without a doubt, changed the way people or machines interact with each other. But before using them it's crucial to ask yourself if your app really needs a real-time connection. In many cases HTTP works just fine.

Happy coding!