Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketio does not exist in request.environ dictionary #79

Closed
jewishop opened this issue Aug 8, 2012 · 7 comments
Closed

socketio does not exist in request.environ dictionary #79

jewishop opened this issue Aug 8, 2012 · 7 comments

Comments

@jewishop
Copy link

jewishop commented Aug 8, 2012

I am using gunicorn with a socketio.sgunicorn.GeventSocketIOWorker worker to run a django project.
When a websocket request arrives, the "socketio" key does not appear in the request.environ dictionary. What can be the reason for that?

The request.environ dictionary:

{'HTTP_COOKIE': 'csrftoken=ou3ptkud0jYBsJ9hKVmQfhQVcI2a9Xiu; sessionid=3cccb6f0e3a18dac0a4b53f9880eea3d', 'SERVER_SOFTWARE': 'gevent/0.13 Python/2.7', 'SCRIPT_NAME': u'', 'REQUEST_METHOD': 'GET', 'PATH_INFO': u'/socket.io/websocket', 'HTTP_ORIGIN': 'http://127.0.0.1:8000', 'SERVER_PROTOCOL': 'HTTP/1.0', 'QUERY_STRING': '', 'HTTP_SEC_WEBSOCKET_EXTENSIONS': 'x-webkit-deflate-frame', 'HTTP_CONNECTION': 'Upgrade', 'SERVER_NAME': 'localhost', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '8000', 'HTTP_UPGRADE': 'websocket', 'wsgi.input': <gevent.pywsgi.Input object at 0x8da33ec>, 'HTTP_HOST': '127.0.0.1:8000', 'wsgi.multithread': False, 'HTTP_SEC_WEBSOCKET_VERSION': '13', 'wsgi.version': (1, 0), 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': False, 'wsgi.errors': <open file '', mode 'w' at 0xb74db0d0>, 'wsgi.multiprocess': False, 'HTTP_SEC_WEBSOCKET_KEY': 'PYVCqrOJSn1N90lN0igPuA==', 'CSRF_COOKIE': 'ou3ptkud0jYBsJ9hKVmQfhQVcI2a9Xiu'}

@wuzuf
Copy link
Contributor

wuzuf commented Aug 8, 2012

Can you please provide an example? Note that the request.environ dict is updated only here: https://github.com/abourget/gevent-socketio/blob/master/socketio/handler.py#L118. So, after the handshake, for example.

@jewishop
Copy link
Author

jewishop commented Aug 9, 2012

Thanks for the answer. After debugging a bit, I see that when I get a websocket request from the client (this is the client code: https://github.com/codysoyland/django-socketio-example/blob/master/templates/chat.html) then the PATH_INFO value is "/socket.io/websocket/". So when the following conditions are checked:

    # Parse request URL and QUERY_STRING and do handshake
    if request_tokens:
        request_tokens = request_tokens.groupdict()
    else:
        handshake_tokens = self.RE_HANDSHAKE_URL.match(path)

        if handshake_tokens:
            return self._do_handshake(handshake_tokens.groupdict())
        else:
            # This is no socket.io request. Let the WSGI app handle it.
            return super(SocketIOHandler, self).handle_one_response()

then handshake_tokens are false and therefore it considers it as a non socket.io request... What am I doing wrong?

@wuzuf
Copy link
Contributor

wuzuf commented Aug 9, 2012

Without having the server code to reproduce, it will be hard for me to help. When I use websocket transport, here is the path: http://localhost:8000/socket.io/1/websocket/213871869224
Which version of socket.io client are you using? I am quite new to socket.io, maybe the protocol changed. All the tests I did were with socketio 0.9.6. If you want a django app example, there is one in gevent-socketio/examples/django_chat.

@sontek
Copy link
Collaborator

sontek commented Nov 15, 2012

@jewishop Closing this since you haven't responded but feel free to re-issue f you are still having problems and we can help you get it fixed.

@abulte
Copy link

abulte commented Nov 29, 2012

Hi,

I'm getting the same problem with both my custom Flask app and the Flask chat example included (cloned from master today). Socket.io.js is 0.9.6.

Any clues ?

Traceback (most recent call last):
File "chat.py", line 154, in socketio
socketio_manage(request.environ, {'/chat': ChatNamespace}, request)
File "/home/alexandre/pyenv_socketio/local/lib/python2.7/site-packages/socketio/init.py", line 60, in socketio_manage
socket = environ['socketio']
KeyError: 'socketio'

@sontek
Copy link
Collaborator

sontek commented Nov 29, 2012

Usually this means you ran it with a server that wasn't using a SocketIO worker. How are you running the app?

Also, are you sure it was using the latest gevente-socketio package? The requirements.txt had a pegged gevent-socketio version

@abulte
Copy link

abulte commented Nov 29, 2012

You're right, I was not running it the right way :-/ I was using python chat.py instead of python run.py. Working good now, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants