Skip to content

Commit

Permalink
Further update Daphne/Channels for v4
Browse files Browse the repository at this point in the history
- Move routing to asgi.py per Django convention
- Use Consumer.as_asgi() for ASGI 3 compatibility
- Use the new Daphne application server
  • Loading branch information
krishnans2006 committed May 3, 2024
1 parent 6a5a1fc commit e889337
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
48 changes: 44 additions & 4 deletions tin/asgi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
"""
ASGI config for tin project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os
from typing import Optional

from channels.routing import get_default_application
from channels.auth import AuthMiddlewareStack
from channels.generic.websocket import WebsocketConsumer
from channels.routing import ProtocolTypeRouter, URLRouter

import django
from django.core.asgi import get_asgi_application
from django.urls import path

from .apps.submissions.consumers import SubmissionJsonConsumer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tin.settings")
django.setup()
application = get_default_application()


class WebsocketCloseConsumer(WebsocketConsumer):
def connect(self):
self.accept()
self.close()

def receive(self, text_data: Optional[str] = None, bytes_data: Optional[bytes] = None):
pass

def disconnect(self, code):
pass


application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
[
path("submissions/<int:submission_id>.json", SubmissionJsonConsumer.as_asgi()),
path("<path:path>", WebsocketCloseConsumer.as_asgi()),
]
)
),
}
)
37 changes: 0 additions & 37 deletions tin/routing.py

This file was deleted.

4 changes: 2 additions & 2 deletions tin/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
# Application definition

INSTALLED_APPS = [
"daphne",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"channels",
"social_django",
"django_extensions",
"django_celery_results",
Expand Down Expand Up @@ -99,7 +99,7 @@
]

WSGI_APPLICATION = "tin.wsgi.application"
ASGI_APPLICATION = "tin.routing.application"
ASGI_APPLICATION = "tin.asgi.application"


CHANNEL_LAYERS = {
Expand Down
2 changes: 1 addition & 1 deletion tin/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""tin URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
Expand Down
2 changes: 1 addition & 1 deletion tin/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
"""

import os
Expand Down

0 comments on commit e889337

Please sign in to comment.