NilCoalescing / djangochannelsrestframework

A Rest-framework for websockets using Django channels-v4
https://djangochannelsrestframework.readthedocs.io/en/latest/
MIT License
603 stars 84 forks source link

djangochannelsrestframework working with gunicorn #182

Closed AndreaMascoli closed 11 months ago

AndreaMascoli commented 1 year ago

Is your feature request related to a problem? Please describe. I've a project where i use gunicorn in prod environment. I saw that gunicorn is an application server used only for WSGI mode, so it can't substitute something like Daphne. I've implemented in dev environment the consumers and the websocket connection with them, and all seems to work good using only 'python manage.py runserver' and the configuration found on internet to use daphne and djangochannelsrestframework. I know that daphne was projected to work efficiently with channels library. The problem is that i have to repropose this functionality also in prod env and i have to necessarily use gunicorn. On the web i read that is possible to run gunicorn and Uvicorn as a worker class in order to manage the asgi functionality. The possible solutions that i think i can implement are one of this two: 1 Make djangochannelsrestframework work with uvicorn 2 Run gunicorn and daphne in certain way similar to the uvicorn working class So the questions are : is possible to implement at least one of these solutions ? Which would be the best one?

Describe the solution you'd like I want to run gunicorn server and make work consumers and websocket implemented with daphne

Describe alternatives you've considered 1 Make djangochannelsrestframework work with uvicorn 2 Run gunicorn and daphne in certain way similar to the uvicorn working class

hishnash commented 1 year ago

This is a better question for the channels package, the deployment docs are here https://channels.readthedocs.io/en/stable/deploying.html they do mention you can use Uvicorn I myself have never done this in product so cant say for sure how well it will perform.

Most people deploy behind a reverse proxy (like nginx) so they run both the WSGI for Django endpoints and Daphne for channels with engine routing to to the respective local port for each server depending on the request path.

johnthagen commented 1 year ago

You don't need to use Daphne at all if you want to instead use Uvicorn. Uvicorn has a built in Guincorn-compatible worker class:

What I like about this is you can run your full stack in ASGI. Uvicorn will run your Django HTTP ASGI endpoints and your websocket endpoints, and Gunicorn can be used to scale and manage the workers.

All of this can still be behind NGINX to handle HTTPS, static files, etc.