Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On my search for exactly the same thing I found this <a href="http://reminiscential.wordpress.com/2012/04/07/realtime-notification-delivery-using-rabbitmq/">blog post of Kevin Jing Qiu</a>.</p> <p>I went the rabbitmq hole a bit further to give every websocket his own set of channel and queues.</p> <p>The extract from my project can be found below. A tornado application bound to RabbitMQ consists of these parts:</p> <ol> <li>The Tornado Application that will handle web requests. I only see long lived websockets here, but you can do so with short lived http requests as well. </li> <li>A (one) RabbitMQ connection hold by the PikaClient Instance</li> <li>a web connection that defines its channels, queues and exchanges when the open method is triggered.</li> </ol> <p>Now a websocket connection can receive data from tornado (data from the browser) via on_message and send it to RabbitMQ. </p> <p>The websocket connection will receive data from RabbitMQ via basic_consume. </p> <p>This is not fully functional, but you should get the idea.</p> <pre><code>class PikaClient(object): def __init__(self, io_loop): logger.info('PikaClient: __init__') self.io_loop = io_loop self.connected = False self.connecting = False self.connection = None self.channel = None self.message_count = 0 """ Pika-Tornado connection setup The setup process is a series of callback methods. connect:connect to rabbitmq and build connection to tornado io loop -&gt; on_connected: create a channel to rabbitmq -&gt; on_channel_open: declare queue tornado, bind that queue to exchange chatserver_out and start consuming messages. """ def connect(self): if self.connecting: #logger.info('PikaClient: Already connecting to RabbitMQ') return #logger.info('PikaClient: Connecting to RabbitMQ') self.connecting = True cred = pika.PlainCredentials('guest', 'guest') param = pika.ConnectionParameters( host='localhost', port=5672, virtual_host='/', credentials=cred ) self.connection = TornadoConnection(param, on_open_callback=self.on_connected,stop_ioloop_on_close=False) self.connection.add_on_close_callback(self.on_closed) def on_connected(self, connection): logger.info('PikaClient: connected to RabbitMQ') self.connected = True self.connection = connection # now you are able to call the pika api to do things # this could be exchange setup for websocket connections to # basic_publish to later. self.connection.channel(self.on_channel_open) def on_channel_open(self, channel): logger.info('PikaClient: Channel %s open, Declaring exchange' % channel) self.channel = channel def on_closed(self, connection): logger.info('PikaClient: rabbit connection closed') self.io_loop.stop() class MyWebSocketHandler(websocket.WebSocketHandler): def __init__(self): self.status = 'not connected yet' def open(self, *args, **kwargs): self.status = "ws open" self.rabbit_connect() # connect this websocket object to rabbitmq def rabbit_connect(): self.application.pc.connection.channel(self.rabbit_channel_in_ok) def rabbit_channel_in_ok(self,channel): self.channel_in = channel self.channel_in.queue_declare(self.rabbit_declare_ok, exclusive=True,auto_delete=True) # and so on... handlers = [ your_definitions_here_like_websockets_or_such ] settings = { your_settings_here } application = tornado.web.Application(handlers,**settings) def main(): io_loop = tornado.ioloop.IOLoop.instance() # PikaClient is our rabbitmq consumer pc = PikaClient(io_loop) application.pc = pc application.pc.connect() application.listen(config.tornadoport) try: io_loop.start() except KeyboardInterrupt: io_loop.stop() if __name__ == '__main__': main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload