4

Hello I am a beginner in the python world, so I am still trying to understand the care when working with ASGI. I read some tutorials and documentation, as well as watched some videos on youtube. However, I was unsure on some points.

I have a small backend application using Django + Django Rest Framework.

My code is very trivial, composed of the most common concepts in the framework: views, serializers, models, urls, etc. In addition, I use a relational database.

My environment is this:

  • Python 3.8
  • Django 3
  • Django Rest Framework 3.11

Now, I need to add support for WebSockets and I did the basic configuration described in the Django Channels tutorial:

  • I installed Django Channels 2.4.0 (Daphene 2.5.0)
  • Added 'channels' to INSTALLED_APPS
  • I created a routing.py file with an empty ProtocolTypeRouter
  • I added ASGI_APPLICATION to my settings.py
  • I configured the asgi.py file to use channels
  • At the moment, I have not configured any channel layers
  • At the moment, I haven't created any WebSocket endpoint

After these configurations the runserver is using an ASGI development server and apparently my REST endpoints are all working.

Some questions:

  • Considering that all my code is synchronous, wouldn't it be necessary to make any adjustments to it?

  • This configuration above, already does all the magic necessary for my synchronous code to be executed safely in daphene considering that it is an ASGI server?

  • Can I serve normal HTTP and WebSockets requests using only ASGI in a reliable and stable manner? Or, is it recommended to serve HTTP traffic using WSGI and leave only WebSockets traffic to daphene?

  • Where exactly should care be taken regarding synchronous code?

Marlon Patrick
  • 2,366
  • 1
  • 18
  • 26

1 Answers1

1

These are my answers based on previous experiences with Django Channels 2 ...

1) Considering that all my code is synchronous, wouldn't it be necessary to make any adjustments to it?

You can safely keep your existing sync code: no adjustments are required; just make sure to call the “sync version” of django-channels API (i.e. SyncConsumer instead of AsyncConsumer).

On the other hand, Channel Layers uses a different approach, and provides only an async version. When the call is issued from sync code, you need to use the async_to_sync wrapper; for example:

from asgiref.sync import async_to_sync

async_to_sync(channel_layer.group_send)(
    group, {
        "type": 'data_received',
        "content": data,
    })

2) This configuration above, already does all the magic necessary for my synchronous code to be executed safely in daphene considering that it is an ASGI server?

The single missing details is (in settings file):

ROOT_URLCONF = 'project.urls'

3) Can I serve normal HTTP and WebSockets requests using only ASGI in a reliable and stable manner? Or, is it recommended to serve HTTP traffic using WSGI and leave only WebSockets traffic to daphene?

With Channels 2, you can safely choose to use Daphne for both HTTP and WebSockets requests, since Daphne will auto-negotiates between HTTP and WebSocket; this is what I usually do in my projects.

Splitting HTTP and WebSocket traffic, thus:

  • running standard HTTP requests through a WSGI server
  • using Daphne (or uvicorn) only for things WSGI cannot do, like WebSockets, HTTP long-polling or other IoT protocols

is possible, but entirely optional.

Mario Orlandi
  • 5,629
  • 26
  • 29