Skip to main content

A convenience layer atop Pika for use with RabbitMQ topic exhanges.

Project description

PikaTopic - a convenience layer atop Pika for use with RabbitMQ topic exhanges


A RabbitMQ service ( ) must be running.

You also need the pika library.

pip install pika

Run this, it will listen for new messages and not exit unless you kill it.

$ ./examples/

In another window, run this:

$ ./examples/

You should see output from

If your rabbit server is running on a different machine, you can set the PIKATOPIC_HOST environment variable. For example:

$ PIKATOPIC_HOST= ./examples/
$ PIKATOPIC_HOST= ./examples/


You will probably need to set the host name to connect to your RabbitMQ server. The username, password and exchange are all set to defaults which work for a generic server install.

Default values:

  • host is localhost OR the PIKATOPIC_HOST envariable if that is set
  • username is guest
  • password is guest
  • exchange is amq.topic

These may be overridden by passing any of these arguments to the PikaTopic class initialiser or you can change them at the module level by:

import pikatopic

pikatopic.DEFAULT_USERNAME = 'otheruser'
pikatopic.DEFAULT_PASSWORD = 'secrt'
pikatopic.DEFAULT_HOST = ''
pikatopic.DEFAULT_EXCHANGE = 'monster'

Pika Library

Pikatopic depends on the pika libary.

$ pip install pika


from pikatopic import PikaTopic

with PikaTopic(host='') as pt:
        'name':"The Amazing Adventures of Sausage Farts The Dog",
from pikatopic import PikaTopic

def handler(routing_key, message, message_data):
    if message_data:
        print("%r data=%r" % (routing_key, message_data))
        print("%r text=%r" % (routing_key, message))
    return True

with PikaTopic(host='') as pt:
    pt.listen(handler, ['#'])


This library provides a class, PikaTopic which can be used for sending and receiving messages.

Unless using the with construct as in the examples above, you must call open() and close() before and after the send...() and listen() functions.


sendText(routing_key (string), message (string))
sendData(routing_key (string), message (dict or list))

The sendData method converts the message to a json string and sets the message content_type property to application/json.


listen(handler (function), binding_keys (list of strings))

The listen method enters an event loop which normally does not return.

It accepts a handler function which is called for each message received.

handler(routing_key, message_text, message_data)

message_text contains the raw text of the message. If the message is json encoded then message_data contains the decoded dict otherwise it is set to None.

If the handler returns a false value, the listen() loop will return.

Serverless Testing

Pass no_rabbit_server=True to the class initialiser to run without connecting to a server.

This might be useful for testing or transition.

You may want to also set verbose=True


Pass verbose=True to the class initialiser to get messages sent to stdout by the listen() and send…() functions.

Thanks, Artella!

This work was funded by Artella ( ).

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pikatopic, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size pikatopic-1.0.0.tar.gz (4.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page