Simple Channels for Tornado (rev 0.0.1)

I pushed some of the refactored code from my little test project out today.  Can't say I'm in love with the code, nor with how it was written (I'm afraid I hacked it from spike test right up to final implementation).  Anyway, the result is a poll/long-poll/streaming channel-server for Tornado + JQuery.  You use it something like this:

class ChannelHandler( longpoll.LongPollMixin, basehandler.BaseHandler ):
    def can_access( self, sub ):
        '''Override to provide authenication for channels'''
        if not(super(ChannelHandler,self).can_access(sub)):
            return False
        if my_logic_says_this_user_cannot_see_this_channel( sub ):
            return False
        return True

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"^/channels$", ChannelHandler),
        settings = dict(
            # Note: you must copy longpoll.js to the static file directory!
            static_path=os.path.join(os.path.dirname(__file__), "static"),
        tornado.web.Application.__init__(self, handlers, **settings)

With the Javascript looking like this:

    var subscriptions = $.longPollSubscriptions( {
        url: '/channels',
        stream: true
    } );
    subscriptions.subscribe( 'some-channel', function( msg ) { } );

You write standard Handlers to handle initial state transfer, posting messages to the channel, etceteras, so you use your application's normal authorization scheme for that. To write to a channel:

torchannels.channels.Channels.get_channel( channel_name ).send( { } )

Anyway, the code is extremely alpha, but it's up on LaunchPad if you want to play with it.

bzr branch lp:torchannels

Have fun.


Comments are closed.


Pingbacks are closed.