gevent-websocket as middleware (to use with uwsgi)

1,367 views
Skip to first unread message

Damjan

unread,
May 11, 2012, 7:31:22 PM5/11/12
to gev...@googlegroups.com
The current gevent-websocket implementation (https://bitbucket.org/Jeffrey/gevent-websocket/) requires you to setup a special pywsgi server. But how can I use it with the uwsgi server (which now supports the gevent loop)?

Is websocket implementable on the wsgi middleware layer, so that I could do:

app = websocket.wrap(my_app)

?

--
damjan

Jeff Lindsay

unread,
May 11, 2012, 9:43:03 PM5/11/12
to gev...@googlegroups.com
End of the day so I might be wrong, but because there is no standard way for servers to hand off their socket object in an upgrade situation, you can't have a general wsgi middleware for handling websocket. That said, the WebSocket-For-Python project has a wsgi middleware solution for gevent based on a simple ad-hoc "extension" protocol I made that will put the socket object into the wsgi environment if there is an upgrade header. This requires me to implement a special pywsgi server as well ... but the point is that if you could hack uwsgi to use the same protocol, then you could use the same middleware in both.

Unfortunately, gevent-websocket and friends didn't seem to be too into this idea. I wanted to see if we could get this hack into pywsgi server proper so we can have multiple websocket projects as middleware that use the same protocol.

So, short answer is: you can't, but you can try and use WebSocket-For-Python and hack uwsgi to support generic upgrade handling the same way I did with pywsgi ... then you'd at least be able to use a library and not make your own websocket handler specific to uwsgi. This scenario is exactly why I implemented the gevent server support in ws4py the way I did.

-jeff
--
Jeff Lindsay
http://progrium.com

CMGS

unread,
May 12, 2012, 9:57:15 AM5/12/12
to gev...@googlegroups.com
here is a gist I think u can use it。

I post it in gunicorn issue, however, it can use everywhere.

first, you write a code like

def handle(ws):
    ws.recv()
    ws.send('xxx')

then, 

app=websocket.WebSocketWSGI(handle)
--

-CMGS

A simple coder.
Love travel, sports especially outdoor sports and computer technology.
Have a dream that one day can tour around.

Damjan

unread,
May 12, 2012, 10:17:05 AM5/12/12
to gev...@googlegroups.com
End of the day so I might be wrong, but because there is no standard way for servers to hand off their socket object in an upgrade situation, you can't have a general wsgi middleware for handling websocket.

So, maybe we need an extension to the WSGI protocol for the servers to supply the underlying socket.
Anyone know if there's been some talk about that?


--
damjan

Jeff Lindsay

unread,
May 12, 2012, 4:56:38 PM5/12/12
to gev...@googlegroups.com

So, maybe we need an extension to the WSGI protocol for the servers to supply the underlying socket.
Anyone know if there's been some talk about that?

I haven't seen/found any, but this is what I came up with as a proposal:

 

--
damjan

Gavin Wahl

unread,
Sep 24, 2012, 9:37:05 PM9/24/12
to gev...@googlegroups.com
I wrote a WSGI middleware that wraps gevent-websockets to allow it to be used with uwsgi: 

I think it should be more robust than the other solutions in this thread, given gevent-websocket's maturity.

Benoit Chesneau

unread,
Sep 25, 2012, 1:45:51 AM9/25/12
to gev...@googlegroups.com

Agree. We should at least a common spec to expose the socket. maybe we should discuss it on the python ml?

- benoît
> --
> damjan

Roberto De Ioris

unread,
Sep 25, 2012, 2:39:47 AM9/25/12
to gev...@googlegroups.com
Sadly this has been already discussed on WEB-SIG, and (as always, sorry
for being grumpy) died there.

I would be very happy to agree on a common way, if uWSGI and gunicorn use
the same standard i suppose the other will follow pretty soon.


--
Roberto De Ioris
http://unbit.it
Reply all
Reply to author
Forward
0 new messages