Tushar Kulkarni
Tushar Kulkarni

Reputation: 148

How to request to Gunicorn and respond with Daphne? Using Django channels, Nginx, Gunicorn, Daphne

I'm facing a strange issue in deploying Django app with Nginx, Daphne and Gunicorn.

As of now, I have successfully deployed my Django app. My HTTP requests are handled by Gunicorn, and WSS requests are handled by Daphne, which is meant to happen.

Consider a scenario, where I send an HTTP request to Gunicorn, and Django needs to send a message back via channels. How do I do that?

For example, if I add a post via HTTP POST request, and want to send a notification via channels to all the open channels (which I have a list of) how do I make Django send back a channel message through Daphne, even when the request was HTTP in the first place received through Gunicorn?

The request flow that I need is:

Client > HTTP > NGINX > GUNICORN > DJANGO > Do some processing > Send notification to all open channels via WSS > Send HTTP RESPONSE OK STATUS=200 to client

I used this guide to deploy

https://github.com/mitchtabian/HOWTO-django-channels-daphne/blob/master/README.md

Everything is working fine except I don't receive WSS messages on the client WebSocket listener, when I send a HTTP request to Gunicorn, but when I send HTTP request to Daphne I get a WSS response successfully

Upvotes: 0

Views: 875

Answers (1)

florianvazelle
florianvazelle

Reputation: 313

You need to configure a Channel Layer:

Channel layers allow you to talk between different instances of an application.

channels_redis is a good one, to transfer a message from a server to another.

The request flow will be:

Client request -> NGINX -> Gunicorn -> Django Application -> Send notification  -> Redis -> Websocket Consumer -> Send response

Upvotes: 0

Related Questions