Whenever you hear the word real-time, the first thing that crosses your mind(If you are a developer) is a Web Socket Connection.
Django doesn’t support web sockets and this is where Django-Channels comes to my rescue.
Below content is for Django-channels version 2.x and Python version 3.x.
group_add
method of channel_layer and in order to remove a connection from group use the group_discard
method of the channel layer.group_send
method of the channel layer.connect
method of my consumer. Learn more about Consumers here.group_add
method. For code, see the snippet at the end.disconnect
method of the consumer.disconnect
method, I use the group_discard
method to remove the connection from the group. See below snippet for the code.So by using Django-Channels, I am able to complete my task of sending the notifications in Real-time.
I have also used Channels to create a real-time messaging app and it’s working seamlessly.
Hope you enjoyed the article. Cheers!
Also read: Getting Started with Two-Factor Authentication in Django Admin Panel