Django Channels alongside Django Rest Framework

764 views
Skip to first unread message

adam....@hedgehoglab.com

unread,
Jan 31, 2017, 7:09:17 AM1/31/17
to Django users
Hi everyone,

We are currently considering using Django Channels for implementing WebSockets into a project. I am wondering if it is known that Django Channels runs perfectly within the same Django project as DRF? 

I have spent a little time investigating this myself and could manage to get DRF working on its own, but channels not working, and channels working on its own but not DRF (in the same project), depending on whether I ran the daphne server or 'runserver' which I assume runs WSGI. Couldn't find anything online about getting these two side by side.

Thanks,
Adam

======================================================

hedgehog lab is a limited company registered in England and Wales. Registered number: 05993194. Registered office: Generator Studios, Trafalgar Street, Newcastle Upon Tyne, NE1 2LA

Andrew Godwin

unread,
Jan 31, 2017, 1:20:49 PM1/31/17
to django...@googlegroups.com
Hi Adam,

The two should be able to coexist without any problem. If you put channels into INSTALLED_APPS then "runserver" is actually running Daphne already anyway, so it's worth noting if that's the case.

You have two options:
 - Run everything via Daphne, which will serve both HTTP and WebSocket requests
 - Run WebSockets via Daphne, and HTTP to a WSGI server, and use something like nginx in front to split the traffic based on path or header.

If you let me know the symptoms of how DRF didn't work via Daphne, I can hopefully help you get things working.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f13e0afc-98a4-4b4c-9315-48f95607e681%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

adam....@hedgehoglab.com

unread,
Feb 3, 2017, 10:07:18 AM2/3/17
to Django users
Hi Andrew,

Thanks for the reply.

I've managed to get it working running Daphne serving both HTTP and WebSocket requests. Your reply prompted me to look into what I was currently doing, and it crossed my mind that I was running via Gunicorn which from now reading the deployment documentation for Django Channels states WebSockets won't work via Gunicorn. 

I'm now running it via a systemctl script using -

ExecStart=/var/www/myproject/env/bin/daphne -b 0.0.0.0 -p 8000 -u /tmp/myproject.sock myproject.asgi:channel_layer
ExecStartPost=/var/www/myproject/env/bin/python3 manage.py runworker

This is then reverse proxied through nginx.

Hopefully this is the best way to go about it when running inside of a systemctl service, although I couldn't find any documentation on this so I adapted how tutorials / examples use a systemctl service for Django with Gunicorn.

Thanks for the prompt response and appreciate the work done for Django!

Adam

Alexandre Verri

unread,
Dec 12, 2017, 12:37:26 PM12/12/17
to Django users
Hi Adam, 

are you using Django REST Framework? 

I was using it, but after introducing Django Channels the REST endpoints stopped to work. 

Regards,
Alexandre
Reply all
Reply to author
Forward
0 new messages