From b23ce26055d0acef27773e212d2c84b91e489ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Wed, 17 May 2023 15:18:15 +0200 Subject: [PATCH] Upd. --- content/django-server-sent-events.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/content/django-server-sent-events.md b/content/django-server-sent-events.md index 616bd63..7ffe62a 100644 --- a/content/django-server-sent-events.md +++ b/content/django-server-sent-events.md @@ -110,15 +110,21 @@ Running the above view with the runserver results in the following error: consume asynchronous iterators in order to serve them synchronously. Use a synchronous iterator instead. -So I had to result to installing uvicorn and run my project as so: +Fortunately Daphne, the ASGI server which was developed to power Django Channels, has an async runserver which we can use: - :::bash - $ uvicorn --log-level debug --reload --timeout-graceful-shutdown 0 project.asgi:application` +To set this up we'll have to install the `daphne` package, add `daphne` to the top of our installed apps, and set +the `ASGI_APPLICATION` setting to point to our ASGI application. -The `--reload` part is particulary important when doing development, but it -does not work well when working with open connections since the server will -wait for the connection to close before reloading. This is why -`--timeout-graceful-shutdown 0` is needed. + :::python + INSTALLED_APPS = [ + "daphne", + ... + "chat", # Our app + ] + + ASGI_APPLICATION = "project.asgi.application" + +Now we can just run `./manage.py runserver` as before and we are async ready! ### More old tech to the rescue: PostgreSQL LISTEN/NOTIFY