Daphne - ASGI Server

4.2.1 · active · verified Thu Apr 09

Daphne is a pure-Python HTTP, HTTP/2, and WebSocket protocol server for ASGI (Asynchronous Server Gateway Interface) applications. It is primarily developed to power Django Channels and acts as the reference server for ASGI. It supports automatic negotiation of protocols, removing the need for URL prefixing for different endpoints. The current version is 4.2.1.

Warnings

Install

Quickstart

Daphne is typically run from the command line, pointing it to your ASGI application. For a Django project, this involves creating or modifying the `asgi.py` file to define your ASGI application, then invoking Daphne as shown below. The example `asgi.py` demonstrates a basic setup for a Django project without WebSockets.

# myproject/asgi.py
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.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()

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    # Add WebSocket protocol here if using Django Channels
    # "websocket": URLRouter(websocket_urlpatterns)
})

# To run:
# Ensure myproject is on the Python path (e.g., run from parent directory of myproject)
# daphne -b 0.0.0.0 -p 8000 myproject.asgi:application

view raw JSON →