arthur.sw
arthur.sw

Reputation: 11639

Multiple namespace in gevent-socketio on django

I managed to make an application similar to the django chat example of gevent-socketio. However, when I add a namespace (class) in sockets.py I have the socketio error: 'no_such_namespace'.

I need to add

socketio_manage(request.environ, { '/chat': ChatNamespace ,'/other': OtherNamespace },request)

in my view, but then I have the following KeyError:

File "...socketio/__init__.py", line 67, in socketio_manage
    socket = environ['socketio']
KeyError: 'socketio'

Upvotes: 2

Views: 467

Answers (1)

Sean
Sean

Reputation: 1

There are a few weird things that you have to get right in order for Gevent Socketio to work with Django.

The first thing you should do is make sure you're using the Socketio client version 0.9.6 (it should say in the source code which version you're using). This is because the newer versions of the Socketio client format their GET and POST requests in a way that Gevent Socketio doesn't recognize, and the server just assumes that they are normal Django requests and doesn't complete the handshake (which is why there is no 'socketio' key in the environ).

The second thing you should do, but technically don't have to, is add this to your root URL conf:

import socketio.sdjango
socketio.sdjango.autodiscover()

This will automatically locate any namespaces (in sockets.py) and register them, and will also take care of your first line:

socketio_manage(...)

If for some reason the namespaces are not being automatically registered, it may because you didn't add the namespace decorator to each namespace. In your case, simply make sure that your code matches the following:

from socketio.sdjango import namespace

@namespace('/chat')
def ChatNamespace(...):
    ...

@namespace('/other')
def OtherNamespace(...):
    ...

I understand that this question is nearly two years old, but I recently worked through these same problems. So consider this answer for anyone who stumbles upon this in the future.

Upvotes: 0

Related Questions