In this tutorial, we'll use the Django channel Create an application that updates the user login and logout list in real time.
Use WebSockets in Django channel The framework manages the communication between the client and the server, and whenever a user is granted login permission, the event is broadcast to every online user. Each user can automatically access the changes happening in real time on the screen without having to log back in.
Note: We recommend that you have experience with Django before you start reading this tutorial, so you should be familiar with some of the concepts of WebSockets.
We need to use the following tools to create the application.
Django Channels (v1.0.3)
After completing this tutorial, we were able to ......
1.Adding Web Sockets to a Django project via Django Channel
2.Create a simple connection between Django and a Redis server
3.Implement basic user login authentication
4.Feedback actions are triggered via Django every time a user logs in or out
First, create a new virtual environment independently to install some of the project's dependencies.
Then, install Django, Django Channel, and ASGI Redis, and create a new Django project and application.
Note: In the course of practicing this tutorial, we will be creating a lot of different files and folders. If you get stuck somewhere, please refer to the file structure of the project repository.
Next, download and install Redis. If your computer is a Mac, we recommend using Homebrew for the installation of.
Next, start a separate terminal to run the Redis server program. Make sure the default port number for running the program is 6379, as this port is critical when we ask Django to interact with Redis. The initialization setup is complete after updating the INSTALLED_APPS in the project settings.py file as follows.
Then configure CHANNEL_LAYERS by setting the default BACKEND and ROUTING
As needed for the experiment, Redis is used here as the backend
Typically, Django uses the HTTP protocol for client-server communication: the
1.The client sends an HTTP request to the server
2.It's up to Django to parse this request, extract the URLs from it, and pair them into a view
3. After the request is processed by the view, an HTTP response is returned to the client
Note: Unlike the HTTP protocol, the WebSockets protocol allows for bi-directional data transfer, meaning that the server can transfer data to the client without the user requesting it, whereas in the HTTP protocol only the client that sends the request will receive the response. With the use of WebSockets, the server can interact with multiple clients at the same time. In this tutorial, we use the prefix ws:// to deliver WebSockets messages, to distinguish them from http://
Note: Before you get into the pit, please quickly skimChannels Concepts the document of the
Consumers and Groups
Create the firstconsumer is used to handle the basic connection between the client and the server. A new file is created in the directory example_channel/example calledconsumers.py which reads as follows.
Consumers are paired with Django's view. Whenever a user connects to our application, it is added to the 'users' group and receives information from the server. When the client closes the connection to our application, the channel is removed from the group and the user no longer receives information from the server. Next, let's set up the route, which is almost identical to the Django URL configuration, to create a file called a routing.py under the example_channels directory and add the following code:
So we replaced urlpatterns with channel_routing and url() with route(). At this point we find that the comsumer function is already associated with WebSockets.
Let's write some HTML code that can be used to transfer data to and from the server via WebSocket, recursively creating templates/example in the example_channels/example directory, and finally composing the directory "example_channels/example/templates/example". The HTML code is as follows.