Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

Adam Zedan-2
I am currently using Django channels for websocket communication. I read this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 [hidden email].
To post to this group, send email to [hidden email].
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/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

Aldian Fazrihady
Does it really use Django channels?  I am using Django channels and following its suggested package:  ASGI provided by Daphne

Regards, 

Aldian Fazrihady

On Sat, 23 Mar 2019, 07:58 Adam Zedan, <[hidden email]> wrote:
I am currently using Django channels for websocket communication. I read this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 [hidden email].
To post to this group, send email to [hidden email].
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/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

aishtiaque
Aldian has pointed it out already, but I also use Nginx + gunicorn + daphne for my production server. Nginx decides when to upgrade a request to wss:// and passes it on to my daphne instance to handle automatically. Would be happy to share my config for that if you want.

Best,
Ahmed

On Fri, Mar 22, 2019 at 10:15 PM Aldian Fazrihady <[hidden email]> wrote:
Does it really use Django channels?  I am using Django channels and following its suggested package:  ASGI provided by Daphne

Regards, 

Aldian Fazrihady

On Sat, 23 Mar 2019, 07:58 Adam Zedan, <[hidden email]> wrote:
I am currently using Django channels for websocket communication. I read this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 [hidden email].
To post to this group, send email to [hidden email].
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/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAKizqR7t%2B1tOinqkhigzPTcmT3BuZEwSW7Pw%3D2bMN_9Vr0dEdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

Adam Zedan-2
Hi Ahmed I am a little confused here. I was using NGINX and uwsgi.  Now based on your response i decided to use Daphne.. So Now I have NGINX with Daphne. I am not sure where gunicorn fits in here . I am following this tutorial. Essentially now I am getting a 502 Bad gateway when accessing my website.

This is the nginx configuration I have

server {
   
# the port your site will be served on
    listen      
80;
    server_name
.MyDomain.com;
    charset     utf
-8;


   
# max upload size
    client_max_body_size
75M;   # adjust to taste


   
# Django media
    location
/media  {
       
# your Django project's media files - amend as required
       
alias /home/ec2-user/MyDomainVenv/MyDomainWeb/media;
   
}


    location
/static {
       
# your Django project's static files - amend as required
       
alias /home/ec2-user/MyDomainVenv/MyDomainWeb/static;
   
}




        location
/ {
            proxy_pass http
://0.0.0.0:8001;
            proxy_http_version
1.1;
            proxy_set_header
Upgrade $http_upgrade;
            proxy_set_header
Connection "upgrade";


            proxy_redirect     off
;
            proxy_set_header  
Host $host;
            proxy_set_header   X
-Real-IP $remote_addr;
            proxy_set_header   X
-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X
-Forwarded-Host $server_name;
       
}
}

any suggestions on what might be going wrong ? I tried accessing my website and this is what i got

==> /var/log/nginx/error.log <==
2019/03/23 07:13:21 [error] 22191#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 71.231.182.18, server: MyDomain.com, request: "GET /admin/ HTTP/1.1", upstream: "http://0.0.0.0:8001/admin/", host: "www.MyDomain.com"


==> /var/log/nginx/access.log <==
71.231.182.18 - - [23/Mar/2019:07:13:21 +0000] "GET /admin/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"





On Friday, March 22, 2019 at 10:31:08 PM UTC-7, Ahmed Ishtiaque wrote:
Aldian has pointed it out already, but I also use Nginx + gunicorn + daphne for my production server. Nginx decides when to upgrade a request to wss:// and passes it on to my daphne instance to handle automatically. Would be happy to share my config for that if you want.

Best,
Ahmed

On Fri, Mar 22, 2019 at 10:15 PM Aldian Fazrihady <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mob...@...> wrote:
Does it really use Django channels?  I am using Django channels and following its suggested package:  ASGI provided by Daphne

Regards, 

Aldian Fazrihady

On Sat, 23 Mar 2019, 07:58 Adam Zedan, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">zeda...@...> wrote:
I am currently using Django channels for websocket communication. I read <a href="https://django-websocket-redis.readthedocs.io/en/latest/running.html#django-with-websockets-for-redis-behind-nginx-using-uwsgi" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdjango-websocket-redis.readthedocs.io%2Fen%2Flatest%2Frunning.html%23django-with-websockets-for-redis-behind-nginx-using-uwsgi\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNErA-XHtyLZbr1DxNbiosF-f6F1SA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdjango-websocket-redis.readthedocs.io%2Fen%2Flatest%2Frunning.html%23django-with-websockets-for-redis-behind-nginx-using-uwsgi\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNErA-XHtyLZbr1DxNbiosF-f6F1SA&#39;;return true;">this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 <a href="javascript:" rel="nofollow" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" rel="nofollow" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@....
Visit this group at <a href="https://groups.google.com/group/django-users" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-users/4c4e57fa-c603-4be0-ba1a-cf1e9919fc09%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="37xIQbtCBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django...@....
Visit this group at <a href="https://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-users&#39;;return true;">https://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-users/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-users/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-users/CAN7EoAZ3eoAW8XBKjBP5W11%2BxpOmQFPZt9t86fSWNR8twQ7QRA%40mail.gmail.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/ce2ff4cc-1358-49f9-bbb8-e5d1a4ddc223%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

Adam Zedan-2
In reply to this post by Adam Zedan-2
YES it worked. I used Daphne with nginx and it worked. Thank you.


On Friday, March 22, 2019 at 5:58:20 PM UTC-7, Adam Zedan wrote:
I am currently using Django channels for websocket communication. I read <a href="https://django-websocket-redis.readthedocs.io/en/latest/running.html#django-with-websockets-for-redis-behind-nginx-using-uwsgi" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdjango-websocket-redis.readthedocs.io%2Fen%2Flatest%2Frunning.html%23django-with-websockets-for-redis-behind-nginx-using-uwsgi\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNErA-XHtyLZbr1DxNbiosF-f6F1SA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdjango-websocket-redis.readthedocs.io%2Fen%2Flatest%2Frunning.html%23django-with-websockets-for-redis-behind-nginx-using-uwsgi\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNErA-XHtyLZbr1DxNbiosF-f6F1SA&#39;;return true;">this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 [hidden email].
To post to this group, send email to [hidden email].
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/e4096133-d010-4b96-a403-985fb1b3a104%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching requests from one uwsgi to another uwsgi instance running Django Channels

aishtiaque
Awesome! Sorry I couldn't reply earlier. To explain myself a little better, I use gunicorn to handle all HTTP requests to my server and daphne to handle all WebSocket requests. This isn't necessary since daphne can do both by itself, but the Deploying section of Django Channels docs mentions the following: 

"You can choose to either use Daphne for all requests - HTTP and WebSocket - or if you are conservative about stability, keep running standard HTTP requests through a WSGI server and use Daphne only for things WSGI cannot do, like HTTP long-polling and WebSockets. If you do split, you’ll need to put something in front of Daphne and your WSGI server to work out what requests to send to each (using HTTP path or domain) - that’s not covered here, just know you can do it."

As you can guess, I like stability 😅



On Sat, Mar 23, 2019 at 4:44 AM Adam Zedan <[hidden email]> wrote:
YES it worked. I used Daphne with nginx and it worked. Thank you.


On Friday, March 22, 2019 at 5:58:20 PM UTC-7, Adam Zedan wrote:
I am currently using Django channels for websocket communication. I read this article and it states that I should split the project into two uwsgi instances. It states that

"The web server undertakes the task of dispatching normal requests to one uWSGI instance and WebSocket requests to another one"

Now I have two uwsgi instances running. This is how I am running both.

This uwsgi handles the normal django site requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --socket /home/ec2-user/MyProjVenv/MyProjWeb/site1.socket --chmod-socket=777 --buffer-size=32768 --workers=5 --master --module main.wsgi


This uwsgi handles the websocket requests
uwsgi --virtualenv /home/ec2-user/MyProjVenv --http-socket /home/ec2-user/MyProjVenv/MyProjWeb/web.socket --gevent 1000 --http-websockets --workers=2 --master --chmod-socket=777  --module main.wsgi_websocket

Now the websocket uwsgi launches  main.wsgi_websocket

The code for main.wsgi_websocket one is this
import os
import gevent.socket
import redis.connection
redis
.connection.socket = gevent.socket
os
.environ.update(DJANGO_SETTINGS_MODULE='main.settings')
from ws4redis.uwsgi_runserver import uWSGIWebsocketServer
application
= uWSGIWebsocketServer()

Now after spinning up the two uwsgi instances I am able to access the website.The websocket uwsgi instance is also receiving data however I am not sure if its passing that data to the website uwsgi instance. I am using Django Channels here and this is the configuration I have specified in my settings for Django Channels

CHANNEL_LAYERS = {
   
"default": {
       
"BACKEND": "asgi_redis.RedisChannelLayer",
       
"CONFIG": {
           
"hosts": [(redis_host, 6379)],
       
},
       
"ROUTING": "main.routing.channel_routing",
   
},
}

The channel routing is this
channel_routing = [
    include
("chat.routing.websocket_routing", path=r"^/chat/stream"),
    include
("chat.routing.custom_routing"),
]

and this is the websocket_routing which i have mentioned above



websocket_routing
= [
    route
("websocket.connect", ws_connect),


   
# Called when WebSockets get sent a data frame
    route
("websocket.receive", ws_receive),


   
# Called when WebSockets disconnect
    route
("websocket.disconnect", ws_disconnect),
]

Now the problem is that my ws_receive is never called. If I test on my local dev machine using  "ipaddress:8000/chat/stream" this works perfectly fine however I have no clue why my receive is not being called when I use ipadress:80/ws/ . I am certain that my other uwsgi instance is getting that data but I dont know how to find out if its passing it to the other uwsgi instance of the djnago side and if it is then why is my receive not being called ?. Any suggestions on this would definitely help




--
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 [hidden email].
To post to this group, send email to [hidden email].
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/e4096133-d010-4b96-a403-985fb1b3a104%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAKizqR5k_p_JZy0hSmoPCzHmjV2Ys_uA0Kp_bx7ffZR_%3DJkbRg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.