[uWSGI] How to get and store a websocket session?

timothy lee timlee246 at yahoo.com
Tue Apr 21 06:22:31 CEST 2015

I see. The thing is is that I have another greenlet running that is listening through a zeromq port. And depending on what messages I get from it, I need to send a message to a certain client, and the server can have many clients connected to it. So the only way I can think of to do this is to store the websocket session in a dictionary so I can look up which client to send a message to.  How can I accomplish this if I can't do it the way my example did? 

     On Monday, April 20, 2015 8:35 PM, Roberto De Ioris <roberto at unbit.it> wrote:

> Hello people,
> I can do ws = environ['wsgi.websocket'] when I use gevent's wsgi server,
> but not when I use uwsgi. So how do I get and store a websocket session in
> uwsgi? 
> from gevent import monkey; monkey.patch_all()
> from gevent.pywsgi import WSGIServer
> import uwsgi
> class WebSocketApp(object):
>    def __init__(self):
>        self.my_dict = {}
>        self.id_count = 0
>    def __call__(self, environ, start_response):
>            uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'],
> environ.get('HTTP_ORIGIN', ''))
>            #works when using gevent's WSGI server but not when using
> uwsgi
>            ws = environ['wsgi.websocket']
>            self.my_dict[self.id_count] = ws
>            self.id_count += 1
>            while True:
>                try:
>                    msg = uwsgi.websocket_recv()
>                    uwsgi.websocket_send(msg)
>                except IOError, e:
>                    return ""
>    def send_msg(self, id, msg):
>        self.my_dict[self.id].websocket_send(msg)
> application = WebSocketApp()
> Any Help would be much appreciated.
> Tim
> _______________________________________________
> uWSGI mailing list
> uWSGI at lists.unbit.it
> http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi

They are two different (and unrelated) ways, you cannot use both.

There is no thing like a 'websocket' session in the uWSGI websocket api,
once a thread, a process or a greenlet is marked as a websocket request
(using the handshake function) you can start using send and recv functions
transparently in that process/thread/greenlet.

This is transparent even when you use uWSGI in gevent mode.

Roberto De Ioris
uWSGI mailing list
uWSGI at lists.unbit.it

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.unbit.it/pipermail/uwsgi/attachments/20150421/29317223/attachment-0001.html>

More information about the uWSGI mailing list