Installation
Dependencies
- Django (3.1 and up)
- Django Channels (2.x and 3.x both supported)
- Django REST Framework (3.11 and up)
channels_redisfor channel layer support in production.
Set Up
If you haven't installed and properly configured Django Channels, then make sure to do that before continuing on with Django REST Live.
- Add
rest_liveto yourINSTALLED_APPS.
INSTALLED_APPS = [
# Any other django apps
"rest_framework",
"channels",
"rest_live",
]
- Create a
RealtimeRouterin your ASGI routing file and addrouter.consumerto the websocket routing you set up with Django Channels. Feel free to choose any URL path, here we've chosen/ws/subscribe/.
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from django.core.asgi import get_asgi_application
from rest_live.routers import RealtimeRouter
router = RealtimeRouter()
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter([
path("ws/subscribe/", router.as_consumer().as_asgi(), name="subscriptions"),
])
),
})
Note: if using Channels version 2, omit the
as_asgi()method.
That's it! You're now ready to configure and use django-rest-live.