gevent-websocket message ordering

209 views
Skip to first unread message

frankentux

unread,
Oct 18, 2011, 4:04:55 AM10/18/11
to gevent: coroutine-based Python network library
I have a very simple gevent-websocket websocket broadcast server. The
idea is that it simply broadcasts messages it receives over zmq/socket
to all connected clients. It kind of works.

I use opera (gevent-websocket doesn't yet support the websocket
implementation in firefox 7 and/or chromium) on various machines and
connect. What I've noticed is:

- for each client that connects, it takes 2 messages over zmq before
the client displays anything

- message ordering is distorted over clients - i.e. if I feed the zmq
relay with 1,2,3...n, then the opera clients display e.g:
received: 1
received: 2
received: 2
received: 4
received: 3
received: 3

I haven't used gevent before, so I think I could be doing something
absolutely and fundamentally stupid. Does anyone see what's wrong or
suggest what I could do instead? Here's the websocket server code:


#!/usr/bin/env python
import zmq
import gevent
from geventwebsocket.handler import WebSocketHandler
from gevent.pywsgi import WSGIServer

connections = []

def app(environ, start_response):
context = zmq.Context()
ws = environ['wsgi.websocket']
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, '')
socket.connect('tcp://localhost:3030')
connections.append(ws)
while True:
msg = socket.recv()
for c in connections:
c.send(msg)
gevent.sleep(0.1)



if __name__ == '__main__':
print 'here we go...'
server = WSGIServer(('', 4040), app,
handler_class=WebSocketHandler)
server.serve_forever()

Denis Bilenko

unread,
Oct 18, 2011, 4:16:23 AM10/18/11
to gev...@googlegroups.com
On Tue, Oct 18, 2011 at 3:04 PM, frankentux <cfarre...@googlemail.com> wrote:
> I have a very simple gevent-websocket websocket broadcast server. The
> idea is that it simply broadcasts messages it receives over zmq/socket
> to all connected clients. It kind of works.

> import zmq

Should not you use gevent-zeromq here?
https://github.com/traviscline/gevent-zeromq

frankentux

unread,
Oct 18, 2011, 6:38:11 AM10/18/11
to gevent: coroutine-based Python network library


On Oct 18, 10:16 am, Denis Bilenko <denis.bile...@gmail.com> wrote:
> On Tue, Oct 18, 2011 at 3:04 PM, frankentux <cfarrell1...@googlemail.com> wrote:
> > I have a very simple gevent-websocket websocket broadcast server. The
> > idea is that it simply broadcasts messages it receives over zmq/socket
> > to all connected clients. It kind of works.
> > import zmq
>
> Should not you use gevent-zeromq here?https://github.com/traviscline/gevent-zeromq

Thanks! That was it. Also, there is no need to append the connecting
clients to a list and to loop through the list to broadcast.
With the single edit that you suggested, everything works.

#!/usr/bin/env python
- import zmq
+ from gevent_zeromq import zmq
Reply all
Reply to author
Forward
0 new messages