Introduction to Websockets and Django Channels

We all have seen a website, know about what HTTP servers are, but what are these websockets and how are they even related to HTTPs you must be wondering this. What are Channels? Let’s understand each and every thing in detail. But before understanding all these we need to know about Asynchronous and Synchronous, now you must be thinking of another new term, don’t worry we will catch you up with all these. In this blog we will be having Introduction to Websockets and Channels.

Synchronous

Django web pages are mostly synchronous. Synchronous means occurring or you can say existing at same time period, here we are referring web pages as synchronous which means when a user sends a HTTP request to the web page it collects the request and waits for the reply of that request and returns that reply to the server. Consider an example of when you login or sign up somewhere you enter your credentials submit it to the server, server take that request process it and if everything is ok then it will redirect you to a new page.

 Introduction to Websockets and Channels

What happens in a http request is a user browses the website and retrieves the messages/ data. If he sends an HTTP request to get the data, then the server will return the data. Now consider the same HTTP server for sending messages over a chat room.

 Introduction to Websockets and Channels

In the above picture you can see that when User 1 will send a message to the server and if he refreshes the browser he can see the message and when User 2 will enter the same chat room he first has to request the server for the messages then he will be able to see it. But this is not how we use a chat app; we never refresh it every time when we want to get a new message. So here asynchronous comes into picture.

Asynchronous

Asynchronous allows us to work in real time. In asynchronous we send the request and carry on our execution, in this case we don’t wait for the server to return the request. A Chat App that we use in our daily lives can be considered as an example of Asynchronous. Where two person sends message to each other at same time.

Websockets

In asynchronous  User 1 can send a message to the server and User 2 can retrieve the same message without refreshing the browser every time.

Hope you got a better idea about what are asynchronous and synchronous. Let’s visualize it for better understanding.

Introduction to Websockets and Channels

In Synchronous one request is handled at a time.

In Asynchronous at a time more than one requests are handled.

What are Websockets ?

Websockets are just similar to HTTP but they are used asynchronously instead of synchronously where server and client can send messages at any time. The websocket protocol allows bidirectional communication, where a client and server can communicate with each other freely at the same time. Websockets are compatible with all browsers. We send websocket messages using ws:// instead of http:// .

How does it work?
  1. A client sends an HTTP request to the server.
  2. A connection for websocket established.
  3. Exchange of Information takes place.
  4. Client / server disconnects the connection.
Introduction to Websockets and Channels

In the picture given below you can see that the user will send an HTTP request that will be handled by WSGI and then we will be routed to our views similarly when a websocket is created it is handled by ASGI and then it will be routed to our consumers.

WSGI and ASGI

What are Channels?

Now we know websocket helps multiple users to send messages at the same time but in that multiple users how do we know which user is sending a message, here channels comes in the picture. Channels basically extend the django properties to handle HTTP in addition to handling other protocols as well such as websockets, chatbot, IOT etc. Channel takes Django’s core, providing a layer to the fully asynchronous layer underneath. It runs Django in a synchronous mode and handles sockets and connections asynchronously.

Introduction to Websockets and Channels

Consider an example where a user will send a message and that same message is sent back to all the users registered in our server but we don’t want that right ? So the channel uses groups, it adds users’ channels into the group and sends messages to that group so instead of sending the same message to individuals we will be sending it in a group.

Channels

Conclusion

So, we learned about synchronous, asynchronous, websockets and channels. I hope this blog will be useful for you and helps you to understand each and every step. Hope all your doubts get cleared. Thank you.