
When a new Websocket client connects to the server, we'll receive a 'nnect' event. Then we'll act on the contents of the event, and send the response to the client. Inside that loop, we'll wait for any new events that the server receives from the client. Inside of the websocket_application function, we're going to define an indefinite loop that will handle Websocket requests until the connection is closed. We're going to listen for all Websocket connections, and when the client sends the string "ping", we'll respond with the string "pong!". Next, let's implement some logic for our Websocket application. websocket import websocket_applicationĭjango_application = get_asgi_application ( ) async def application (scope, receive, send ) : if scope = 'http' : await django_application (scope, receive, send ) elif scope = 'websocket' : await websocket_application (scope, receive, send ) else : raise NotImplementedError ( f"Unknown scope type " ) # websocket.py async def websocket_application (scope, receive, send ) : pass The resulting asgi.py file should look something like this:įrom websocket_app. If the request type is 'websocket', then we'll want to handle the logic ourselves.

If the request type is 'http', then the request is a normal HTTP request and we should let Django handle it. Inside of our application function we'll check the value of scope to determine the request type. Rename the result of the get_asgi_application call to django_application, because we'll need it process HTTP requests. To do this, we'll need to define an async function called application, that takes in the 3 ASGI parameters: scope, receive, and send.

In our asgi.py file, we're going to wrap Django's default ASGI application function with our own ASGI application in order to handle Websocket connections ourselves. Let's take a look at how this works in a sample application. When you're ready to send data to the client, you can await the send function, and pass in any data you want to send to the client. To listen for data from the client, you can await the receive function. For example, you can check whether the request is an HTTP request or a Websocket request by checking the value of scope. Inside of an ASGI application, you can route requests based on values in the scope dictionary. ASGI lets you use Python's native async/ await functionality to build web services that support long-lived connections, such as Websockets and Server Sent Events.Īn ASGI application is a single async function which takes in 3 parameters: scope (the context of the current request), receive (an async function that lets you listen for incoming events), and send (an async function that lets you send events to the client).


It's the spiritual successor to WSGI, which has been used by frameworks like Django and Flask for a long time. ASGI app structureĪSGI, or the Asynchronous Server Gateway Interface, is a specification for building asynchronous web services with Python. Before we go much further, let's take a look at how ASGI applications are structured. This file provides the default Django ASGI setup, and exposes an ASGI application called application which can be run using an ASGI server such as uvicorn or daphne. setdefault ( 'DJANGO_SETTINGS_MODULE', 'websocket_app.settings' )
