Build Real Time Notification With Django Channels In 5 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to this tutorial in this video we are going to create a simple notification system with django channels if you are new here my name is ali company i'm a full stack django and view developer and here we are going to talk about programming and sometimes ui ux as well if you like this kind of stuff make sure to subscribe okay let's jump to the code the first step is to set up our project in order to save time i'm using my starter django and docker project which i built based on best practices in the previous video if you haven't watched it i put its link down in the description after that let's create dot emv file and activate our virtual environment now we need to install django channels after that let's head to the settings.py file and add channels to installed apps then we need to edit the ascii.py file in the config folder now let's set the ascii application variable in the settings.py file to point to the routing object as the root of the application after that we need to create a new app let's call it notification and add it to the settings.p wi-fi for handling the front end let's create these folders after that we need to create index.html file and put these codes inside it we simply create a form and get a username from the user then whenever user clicks on the button it must send a notification to all users and tell them for example user a click the button in the script section we use websocket library to first create a connection to ascii server and then handle sending and receiving messages now we need to add a view to handle this page and also create a url for that in order to handle channels we need redis since we use docker let's add a ready service in our docker compose file after that let's install channels redis package and add its configuration inside settings.p wi-fi okay now let's handle websocket in simple http protocol first web server caches the requests and passes them to django after that django based on the url invokes the appropriate view similar to views in order to handle websocket connections with channels we have to create consumers so let's create a file called consumers.py inside the notification directory first we import these dependencies inside our consumers class we need to define four asynchronous methods first one is connect inside it we call the group add method and join to a group called notification the next method is disconnect as the name implies this method calls whenever a socket connection terminates inside it we call group discard and leave the notification group after that we need to create receive method in order to handle receiving messages from websocket we first extract the message after that i create a dictionary called event event has a special type key corresponding to the name of the method that should be invoked on consumers that receive the event then we call the group send method and pass the group name and event dictionary to it and finally let's create send message method which we used inside the event dictionary inside it we send the message to websocket okay now let's create a file called routing to match a url with our consumer class the next step is to point the root routing configuration at the notification routing module so in the ascii.py file we need to first import some dependencies then insert a websocket key in the protocol type router list in the following format protocol type router will first check the type of connection if it is a websocket connection then the connection will be given to the auth middleware stack and after that the url router will examine the http path of the connection to route it to a particular consumer so in less than 5 minutes we created a notification functionality with django channels if you like this video don't forget to like and subscribe and until the next one goodbye and have a nice day
Info
Channel: Webkar Academy
Views: 3,003
Rating: undefined out of 5
Keywords: Ali Kompany
Id: sthCUcw5Zog
Channel Id: undefined
Length: 6min 2sec (362 seconds)
Published: Wed Jul 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.