[Twisted-web] Coding websockets server.

Reza Lotun rlotun at gmail.com
Tue Sep 6 04:06:36 EDT 2011


I've done work on the txWebSocket project in the past - it's now slightly
out of date for thew new spec. You're using the fork by wulczer which
supports the new protocol, so that's good. I have to update the main project
at some point to incorporate his work.

Anyway, to answer your quesions...

> My problem is:
> If I do this:
>     site = WebSocketSite(root)
>     site.addChatHandler('/room*1*', Chathandler)
>     site.addChatHandler('/room*2*', Chathandler)
>     site.addChatHandler('/room*3*', Chathandler)
>     reactor.listenTCP(8080, site)
> will not work (every room will comunicate with every room), because
> the users = set() will be globally between the handlers..
> If I put on the __init__, every call on /roomX will have your personal
> set() of users including only themselfs.
Right, I see what your issue is. You really want a mapping from room
resources to state (i.e. '/room1' -> users1, etc). As you've noticed the
users set is a class-level construct, while the __init__ for the handler is
called on every connection. What you really want to be able to do is
associate this resource mapping on the site object. Unfortunately there is
currently no direct way to gain access to this object, and there is no easy
way to determine the resource you've been called at. I'll need to add this

For the time being I suggest to subclass WebSocketSite, and then access the
object via private access (I know this is *bad*, but there will be a better
option soon) in the handler as self.transport._request.site and the uri as
self.transport._request.uri. That way you can access a mapping on the site

I plan on making access to the site and uri be public which under the hood
will access the above two attributes.

Sorry about the state of txWebsocket - development on it paused as the
websocket spec was going through a state of flux. It seems to have settled
down recently, so now is the time for me to really sort out these issues.
Thanks for bringing it to my attention.

> I am kind stuck on this, I`m new to websocket and server programming, so I
> don`t even know if is the best way to code a chatroom server.
> If you read this far ans have any (even it looks that stupid for you),
> might help right now (:

If you're interested in a basic chatroom implementation, the idea you have
sketched above is fine. However, if you're looking to add a tad more
robustness, might I suggest you implement the fan out logic in Redis using
its pub-sub functionality? Check out http://redis.io/topics/pubsub and a
Twisted library for Redis at https://github.com/rlotun/txRedis. Websockets
and your txWebsocket code can then only need to act as a transport to the
end-user (that is, there is mapping from your getPeer() calls on your
self.transport to users, and every internal subscription per user is then
fed back as websocket data to the browser. That way you can avoid "fan out"
type logic as shown in your sendChat method).

I hope this helps - it isn't entirely satisfying I know, but the situation
will improve soon.


Reza Lotun
mobile: +44 (0)7521 310 763
email:  rlotun at gmail.com
work:   rlotun at twitter.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20110906/0751a814/attachment.htm 

More information about the Twisted-web mailing list