PubSub Module — rabbitChat 1.0.0 documentation

# PubSub Module

The pubsub module provides interface for the rabbitmq client.

It provides classes to create pika clients to connect to rabbitmq broker server, interact with and publish/subscribe to rabbitmq via creating channels, methods to publish, subscribe/consume, stop consuming, start publishing, start connection, stop connection, create channel, close channel, acknowledge delivery by publisher, acknowledge receiving of messages by consumers, send basic ack, basic cancel requests and also add callbacks for various other events.

## RabbitMqClient

class `rabbitChat.apps.rabbitmq.pubsub.` `RabbitMqClient`(credentials=None, params=None, queue=None)

  This is a RabbitMQ Client using the TornadoConnection Adapter that will handle unexpected interactions with RabbitMQ such as channel and connection closures.

  If RabbitMQ closes the connection, it will reopen it. You should look at the output, as there are limited reasons why the connection may be closed, which usually are tied to permission related issues or socket timeouts.

  If the channel is closed, it will indicate a problem with one of the commands that were issued and that should surface in the output as well.

  It alos uses delivery confirmations and illustrates one way to keep track of messages that have been sent and if they’ve been confirmed by RabbitMQ.

  `__init__`(credentials=None, params=None, queue=None)

    Create a new instance of the consumer class, passing in the AMQP URL used to connect to RabbitMQ.

    Parameters: | credentials (pika.credentials.PlainCredentials) – credentials to connect to rabbitmq broker server params (pika.connection.ConnectionParameters) – connection paramaters used to connect with rabbitmq broker server queue (string - random long base64 url safe encoded string) – queue to be created after a channel is established which will be bound to an exchange

  `connect`()

    This method connects to RabbitMQ via the Torando Connectoin Adapter, returning the connection handle.

    When the connection is established, the on_connection_open method will be invoked by pika.

    Returns: | Returns a pika connection object which is a tornado connection object to rabbitmq server
    Return type: | pika.adapters.TornadoConnection

  `on_connection_opened`(connection)

    This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.

    Parameters: | connection (pika.adapters.TornadoConnection) – connection object created

  `add_on_connection_close_callback`()

    This method adds an on close callback that will be invoked by pika when RabbitMQ closes the connection to the publisher unexpectedly.

  `on_connection_closed`(connection, reply_code, reply_text)

    This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.

    Parameters: | connection (pika.connection.Connection) – The closed connection obj reply_code (int) – The server provided reply_code if given reply_text (str) – The server provided reply_text if given

  `reconnect`()

    Will be invoked by the IOLoop timer if the connection is closed. See the on_connection_closed method.

  `close_connection`()

    This method closes the connection to RabbitMQ.

  `open_channel`()

    Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.

  `on_channel_open`(channel)

    This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.

    Since the channel is now open, we’ll declare the exchange to use.

    Parameters: | channel (pika.channel.Channel) – The channel object

  `close_channel`()

    Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.

  `add_on_channel_close_callback`()

    This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.

  `on_channel_closed`(channel, reply_code, reply_text)

    Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.

    Parameters: | channel (pika.channel.Channel) – The closed channel reply_code (int) – The numeric reason the channel was closed reply_text (str) – The text reason the channel was closed

  `setup_exchange`()

    Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC command. When it is complete, the on_exchange_declareok method will be invoked by pika.

  `on_exchange_declareok`(frame)

    Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC command.

    Parameters: | frame (pika.Frame.Method) – Exchange.DeclareOk response frame

  `setup_queue`()

    Setup the queue on RabbitMQ by invoking the Queue.Declare RPC command. When it is complete, the on_queue_declareok method will be invoked by pika.

  `on_queue_declareok`(method_frame)

    Method invoked by pika when the Queue.Declare RPC call made in setup_queue has completed. mand is complete, the on_bindok method will be invoked by pika.

    Parameters: | method_frame (pika.frame.Method) – The Queue.DeclareOk frame

  `bind_queue`(binding_key)

    In this method we will bind the queue and exchange together with the routing key by issuing the Queue.Bind RPC command.

    Parameters: | binding_key (string) – The routing_key argument

  `on_bindok`(unused_frame)

    Invoked by pika when the Queue.Bind method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process. It will also set channel for publishing.

    Parameters: | unused_frame (pika.frame.Method) – The Queue.BindOk response frame

  `setup_publishing`()

    In this method we will setup the channel for publishing by making it available for delivery confirmations and publisher confirmations.

  `enable_delivery_confirmations`()

    Send the Confirm.Select RPC method to RabbitMQ to enable delivery confirmations on the channel. The only way to turn this off is to close the channel and create a new one.

    When the message is confirmed from RabbitMQ, the on_delivery_confirmation method will be invoked passing in a Basic.Ack or Basic.Nack method from RabbitMQ that will indicate which messages it is confirming or rejecting.

  `on_delivery_confirmation`(method_frame)

    Invoked by pika when RabbitMQ responds to a Basic.Publish RPC command, passing in either a Basic.Ack or Basic.Nack frame with the delivery tag of the message that was published. The delivery tag is an integer counter indicating the message number that was sent on the channel via Basic.Publish. Here we’re just doing house keeping to keep track of stats and remove message numbers that we expect a delivery confirmation of from the list used to keep track of messages that are pending confirmation.

    Parameters: | method_frame (pika.frame.Method) – Basic.Ack or Basic.Nack frame

  `publish`(msg, routing_key)

    If the class is not stopping, publish a message to RabbitMQ, appending a list of deliveries with the message number that was sent. This list will be used to check for delivery confirmations in the on_delivery_confirmations method.

    Once the message has been sent, schedule another message to be sent. The main reason I put scheduling in was just so you can get a good idea of how the process is flowing by slowing down and speeding up the delivery intervals by changing the PUBLISH_INTERVAL constant in the class.

    Parameters: | msg – Message to be published to Channel routing_key (string) – Routing Key to direct message via the Exchange
    Tyep msg: | string

  `start_consuming`()

    This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.

  `add_on_cancel_callback`()

    Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.

  `on_consumer_cancelled`(method_frame)

    Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.

    Parameters: | method_frame (pika.frame.Method) – The Basic.Cancel frame

  `on_message`(unused_channel, basic_deliver, properties, body)

    Invoked by pika when a message is delivered from RabbitMQ. The channel is passed for your convenience. The basic_deliver object that is passed in carries the exchange, routing key, delivery tag and a redelivered flag for the message. The properties passed in is an instance of BasicProperties with the message properties and the body is the message that was sent.

    Parameters: | unused_channel (pika.channel.Channel) – The channel object basic_deliver (pika.Spec.Basic.Deliver) – The basic delivery object passed properties (pika.Spec.BasicProperties) – The basic properties used to publish the message body (str|unicode) – The message body

  `acknowledge_message`(delivery_tag)

    Acknowledge the message delivery from RabbitMQ by sending a Basic.Ack RPC method for the delivery tag.

    Parameters: | delivery_tag (int) – The delivery tag from the Basic.Deliver frame

  `stop_consuming`()

    Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.

  `on_cancelok`(unused_frame)

    This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.

    Parameters: | unused_frame (pika.frame.Method) – The Basic.CancelOk frame

  `start`()

    Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.

  `stop`()

    Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.

  `status`()

    Gives the status of the RabbitMQClient Connection.

    Returns: | Returns the current status of the connection
    Return type: | self._status

  `__class__`

    alias of `type`

  `__delattr__`

    x.__delattr__(‘name’) <==> del x.name

  `__dict__` = dict_proxy({'setup_queue': <function setup_queue at 0x10f5c5cf8>, '__module__': 'rabbitChat.apps.rabbitmq.pubsub', 'close_channel': <function close_channel at 0x10f5c5aa0>, 'stop_consuming': <function stop_consuming at 0x10f5c6398>, 'add_on_connection_close_callback': <function add_on_connection_close_callback at 0x10f5c57d0>, 'connect': <function connect at 0x10f5c56e0>, 'reconnect': <function reconnect at 0x10f5c58c0>, '__dict__': <attribute '__dict__' of 'RabbitMqClient' objects>, '__weakref__': <attribute '__weakref__' of 'RabbitMqClient' objects>, '__init__': <function __init__ at 0x10f5c5668>, 'on_consumer_cancelled': <function on_consumer_cancelled at 0x10f5c6230>, 'on_connection_closed': <function on_connection_closed at 0x10f5c5848>, 'on_message': <function on_message at 0x10f5c62a8>, 'publish': <function publish at 0x10f5c60c8>, 'start': <function start at 0x10f5c6488>, 'on_channel_open': <function on_channel_open at 0x10f5c5a28>, 'bind_queue': <function bind_queue at 0x10f5c5de8>, 'on_queue_declareok': <function on_queue_declareok at 0x10f5c5d70>, '__doc__': "\n This is a RabbitMQ Client using the TornadoConnection Adapter that will\n handle unexpected interactions with RabbitMQ such as channel and connection closures.\n\n If RabbitMQ closes the connection, it will reopen it. You should\n look at the output, as there are limited reasons why the connection may\n be closed, which usually are tied to permission related issues or\n socket timeouts.\n\n If the channel is closed, it will indicate a problem with one of the\n commands that were issued and that should surface in the output as well.\n\n It alos uses delivery confirmations and illustrates one way to keep track of\n messages that have been sent and if they've been confirmed by RabbitMQ.\n\n ", 'status': <function status at 0x10f5c6578>, 'stop': <function stop at 0x10f5c6500>, 'on_delivery_confirmation': <function on_delivery_confirmation at 0x10f5c6050>, 'add_on_channel_close_callback': <function add_on_channel_close_callback at 0x10f5c5b18>, 'setup_exchange': <function setup_exchange at 0x10f5c5c08>, 'on_bindok': <function on_bindok at 0x10f5c5e60>, 'setup_publishing': <function setup_publishing at 0x10f5c5ed8>, 'enable_delivery_confirmations': <function enable_delivery_confirmations at 0x10f5c5f50>, 'acknowledge_message': <function acknowledge_message at 0x10f5c6320>, 'on_channel_closed': <function on_channel_closed at 0x10f5c5b90>, 'on_exchange_declareok': <function on_exchange_declareok at 0x10f5c5c80>, 'add_on_cancel_callback': <function add_on_cancel_callback at 0x10f5c61b8>, 'open_channel': <function open_channel at 0x10f5c59b0>, 'on_connection_opened': <function on_connection_opened at 0x10f5c5758>, 'close_connection': <function close_connection at 0x10f5c5938>, 'on_cancelok': <function on_cancelok at 0x10f5c6410>, 'start_consuming': <function start_consuming at 0x10f5c6140>})
  `__format__`()

    default object formatter

  `__getattribute__`

    x.__getattribute__(‘name’) <==> x.name

  `__hash__`
  `__module__` = 'rabbitChat.apps.rabbitmq.pubsub'
  `__new__`(S, ...) → a new object with type S, a subtype of T
  `__reduce__`()

    helper for pickle

  `__reduce_ex__`()

    helper for pickle

  `__repr__`
  `__setattr__`

    x.__setattr__(‘name’, value) <==> x.name = value

  `__sizeof__`() → int

    size of object in memory, in bytes

  `__str__`
  `__subclasshook__`()

    Abstract classes can override this to customize issubclass().

    This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).

  `__weakref__`

    list of weak references to the object (if defined)