jrief / django-websocket-redis

Websockets for Django applications using Redis as message queue
http://django-websocket-redis.awesto.com/
MIT License
895 stars 222 forks source link

clients connect bu disconnect and reconnect every 3 seconds. #186

Closed ezesolares closed 8 years ago

ezesolares commented 8 years ago

Hello.

First, thanks for a great library... now to the problem in hand I'm having my clients keep disconnecting and connecting (every 3 seconds) my configuration is based on the documents (redis nginx and uwsgi).

My setup is as follow

_nginx _

server {
    listen          81;
    charset UTF-8;
    server_name _;
    access_log /var/log/nginx/hpo.com_access.log;
    error_log /var/log/nginx/hpo.com_error.log warn;

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/app/hpo.com/hpo.com.socket;
    }

    location /ws/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://127.0.0.1:3032;
    }
}

uwsgi

websocket
uwsgi --plugin=gevent --virtualenv /home/rtas/webapp/webapp/ --http-socket 127.0.0.1:3032 --chmod=777  --gevent=1000 --http-websockets --workers=2 --master --vacuum --module wsgi_websocket:applicatio

django

uwsgi --virtualenv /home/rtas/webapp/webapp/ --socket /run/uwsgi/app/hpo.com/hpo.com.socket --buffer-size=32768 --workers=5 --master --chmod=777 --module wsgi_djang

Port sniffing

nginx <-> wsgi_websocket.py

now sniffing the websocket port (nginx <-> wsgi_websocket) in port 3032

HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://127.0.0.1:81
Sec-WebSocket-Accept: FnMhrzasPZUnyrbyZysqNpHRU4Y=

j
'NoneType' object has no attribute 'encode'
GET /ws/hpo?subscribe-broadcast&publish-broadcast&echo HTTP/1.1
Upgrade: websocket
Connection: upgrade
Host: 127.0.0.1:3032
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: http://127.0.0.1:81
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: g3jCU79R3KTj7weqCWtP5A==
Cookie: csrftoken=qgMrsgJQFPhvNnLqahmaNat533R5EyGq; sessionid=rglexjsepgy0x8m45k8rb34u5xdzjsg1
Pragma: no-cache
Cache-Control: no-cache

wsgi_websocket <-> redis

and the communication between wsgi_websocket app and redis (in port 6379)

$9
subscribe
$17
hpo:broadcast:hpo
:1

*2
$3
GET
$17
hpo:broadcast:hpo

$-1

*1
$11
UNSUBSCRIBE

*2
$9
SUBSCRIBE
$17
hpo:broadcast:hpo

*3
$9
subscribe
$17
hpo:broadcast:hpo
:1

*2
$3
GET
$17
hpo:broadcast:hpo

$-1

I cant find the problem.. the 'NoneType' object has no attribute 'encode' that shows in the nginx <-> wsgi_django app is bothering me but i added utf-8 in the nginx setup thinking it may be that .. still cant find the right answer.

The setup is a newly installed ubuntu 15.10 with everything updated (nginx from ubuntu package, uwsgi from pip)

Thanks in advance

jrief commented 8 years ago

sorry, I can't follow on this. Are you sure there is no proxy or whatever in between?

ezesolares commented 8 years ago

well, its all on the same computer and nginx acts in between.. its redis <-> wsgi_websocket.py <-> nginx <-> webapp,

I have almost the same setup from the project documents.. except i'm using a socket and not an unix socket for wsgi_websocket and nginx because i wanted to sniff what was happening in between. I'll edit the question better, sorry.. i was running out of time when i posted before having to go to a meeting.

yguarata commented 8 years ago

@ezesolares , I believe that it is happening because your websocket server address:port (127.0.0.1:3032) is different from the app server (127.0.0.1:81), which conflicts with the SESSION_COOKIE_DOMAIN name. I faced the same problem when a I had to put the websocket server in a different host address.

If this is your problem, this pull request must fix it: #185. More info in this issue #184. However, maybe only setting the SESSION_COOKIE_DOMAIN name fixes your problem.

ezesolares commented 8 years ago

@yguarata Well, i had the same issue using unix sockets.... i changed to use sockets only to be able to sniff what was going there using tcpdump. I'll check the pull request

yguarata commented 8 years ago

Updated documentation about this: https://github.com/yguarata/django-websocket-redis/blob/master/docs/installation.rst#running-websockets-in-cross-domains

ezesolares commented 8 years ago

@yguarata i added your pull request but its the same.. i tried using SESSION_COOKIE_DOMAIN='127.0.0.1' and then, also added WEBSOCKET_HOST='127.0.0.1' to my project settings and nothing changes. Connection keep reseting itself, connect and disconnect loop.

yguarata commented 8 years ago

Did you try WEBSOCKET_HOST='127.0.0.1:3230'?

ezesolares commented 8 years ago

I did tried, same thing

Connecting to ws://127.0.0.1:3032/ws/hpo?subscribe-broadcast&publish-broadcast&echo ... ws4redis.js (line 18) Connected! ws4redis.js (line 46) Connection closed! ws4redis.js (line 57) Connecting to ws://127.0.0.1:3032/ws/hpo?subscribe-broadcast&publish-broadcast&echo ... ws4redis.js (line 18) Connected! ws4redis.js (line 46) Connection closed! ws4redis.js (line 57) Connecting to ws://127.0.0.1:3032/ws/hpo?subscribe-broadcast&publish-broadcast&echo ...

ezesolares commented 8 years ago

reading a little bit in the redis_store.py... i get a error in the nginx <-> uwsi_websocket.py communication (check the sniffing)..it says

'NoneType' object has no attribute 'encode'..

Does that have something to do with the heartbeat that maybe is not working here?

yguarata commented 8 years ago

Can you see what happens at this line, during debug? https://github.com/jrief/django-websocket-redis/blob/master/ws4redis/wsgi_server.py#L54

When I had this problem, the server was not able to find the session_key.

yguarata commented 8 years ago

@ezesolares I also had to set the heartbeat message: WS4REDIS_HEARTBEAT = '--heartbeat--'

ezesolares commented 8 years ago

Thanks for taking an interest in my case, im a lil lost

I added a couple of prints because i dont know how else to debug a wsgi app... i'm getting this on console.

request; <WSGIRequest: GET '/ws/hpo?subscribe-broadcast&publish-broadcast&echo'>
session_key: l7pr00c3oe65oywiwlqgbhz9fft3ob8v
settings.SESSION_COOKIE_NAME: sessionid
engine: <module 'django.contrib.sessions.backends.db' from '/home/rtas/webapp/webapp/lib/python3.4/site-packages/django/contrib/sessions/backends
/db.py'>

I'll add the heartbeat message and try

ezesolares commented 8 years ago

Well, adding the WS4REDIS_HEARTBEAT fixed the disconnect issue so far, im getting the heartbeat on my webapp every 3 seconds now... i'm not sure if this would work with more latency considering that i'm testing on the same machine now. I'll open it to the world and see how it works over a mobile connection... and if this fix it.

ezesolares commented 8 years ago

Its working like charm now.. i restarted the hardware today and all the changes i made thanks to this thread are working... its just ocnnected and no heartbeats all the time