djangodjango-channelsdaphneasgi

What is the purpose of using daphne with django channels?


What is the primary purpose of using Daphne with Django channels, if we can already do the ASGI configuration ourselves? What is the relationship between ASGI & Daphne server?


Solution

  • You need a server that speaks and understands ASGI while also being able to speak and understand protocols such as HTTP (the HTTP format covers HTTP/1.0, HTTP/1.1 and HTTP/2.0) and Websocket.

    Your server will run on top of this server and your server will communicate with this server. [1]

    ASGI Rationale

    The ASGI specification proposes a standard interface between network protocol servers (particularly web servers) and Python applications, intended to allow handling of multiple common protocol styles

    Daphne is a HTTP, HTTP2 and WebSocket protocol server for ASGI and ASGI-HTTP, developed to power Django Channels.

    So neither Django nor Django Channels (and by extension your server) by themselves are ASGI servers but are actually ASGI applications. ASGI applications must have an entry point from which the ASGI server can invoke them. The specification defines it to look like this:

    coroutine application(scope, receive, send)

    Not that it must not be exact but the idea is well conveyed. Django provides that entry point. See How to deploy with ASGI. [django-docs]


    [1] Your server (actually Django, Django Channels and any other frameworks that will themselves actually communicate with Daphne) is defined as the application in the ASGI specification whereas Daphne is defined as the server.