Using Daphne for Django Channels
In Chapter 16, Building a Chat Server, you used Django Channels to build a chat server using WebSockets. uWSGI is suitable for running Django or any other WSGI application, but it doesn’t support asynchronous communication using Asynchronous Server Gateway Interface (ASGI) or WebSockets. In order to run Channels in production, you need an ASGI web server that is capable of managing WebSockets.
Daphne is an HTTP, HTTP2, and WebSocket server for ASGI developed to serve Channels. You can run Daphne alongside uWSGI to serve both ASGI and WSGI applications efficiently. You can find more information about Daphne at https://github.com/django/daphne.
You already added daphne==3.0.2
to the requirements.txt
file of the project. Let’s create a new service in the Docker Compose file to run the Daphne web server.
Edit the docker-compose.yml
file and add the following lines:
daphne:
build: .
working_dir: /code/educa/
command...