In one of our application, i was able to run Django with Daphne in hot reload mode (in my case in a docker environment), following this documentation on the official Django site:
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/daphne/#integration-with-runserver
Versions used:
Daphne = 4.0 (check release notes https://github.com/django/daphne/blob/main/CHANGELOG.txt)
Django = 3.2.13 (Versions > 3.0 should work; python > 3.6 is required)
One needs to integrate Daphne with the runserver command.
This can be achieved by adding daphne explicitly to the INSTALLED_APPS of your django settings file (was not required in older daphne versions):
INSTALLED_APPS = [
"daphne",
...,
]
Create or edit your asgi.py file in your django root app:
import os
import django
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings")
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
from my_app.routing import websockets
django.setup()
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": websockets
})
Note: I am also using websockets in my app. In your app you may not need "websocket": websockets in the snippet above.
Start the development server from your application root path with the command:
python manage.py runserver
On the server startup you should see a log like this:
Django version 3.2.13, using settings my_app.settings.development
Starting ASGI/Daphne version 4.0.0 development server at http://0.0.0.0:8000/
Now a daphne server should be running in your development environment and restart on every code change.
Hope this helps someone.