Pubkeeper

Introduction

Pubkeeper attempts to bridge the communication gap between the numerous available messaging protocols by aggregating them into a sensible publisher/subscriber model. Often there is a specific need for using one messaging protocol over another, be it library size or availability on a specific platform. Pubkeeper provides a unified client interface for publishing and subscribing to topics without worrying about different protocols. A given publisher can publish the same information over multiple protocols to allow multiple subscribers to obtain the information. Likewise, a given subscriber can receive data from the same topic via multiple types of publishers.

Example

Pubkeeper enables a developer to set up and configure publishing (brewing) to multiple protocols. Here we're take a single pub/sub topic, test.topic.data, and then register a "brew" for each supported protocol (in this case the protocols, or "brews," are WebSocket and ZMQ), along with configuration options for each brew.

Brewer (publisher)

from pubkeeper.client import PubkeeperClient
from pubkeeper.brewer import Brewer
from pubkeeper.brew.websocket.brew import WebsocketBrew
from pubkeeper.brew.zmq.brew import ZMQBrew
import time

config = {
    'host': 'localhost',
    'port': 9898,
    'secure': False
    'token': 'your-pk-token',
    'bridge_mode': False
}
client = PubkeeperClient(config)

websocket_brew = WebsocketBrew()
websocket_brew.configure({
    'ws_host': 'localhost',
    'ws_port': 9555,
    'ws_secure': False
})

zmq_brew = ZMQBrew()
zmq_brew.configure({})

client.add_brew(websocket_brew)
client.add_brew(zmq_brew)

client.start()

brewer = client.add_brewer('test.topic.data')

try:
    while True:
        brewer.brew(b'data')
        time.sleep(1)
except KeyboardInterrupt:
    client.shutdown()

Contained in the configuration will be the required arguments for a patron (subscriber) to successfully connect to the brewer (publisher). In this way, we can have patrons of WebSocket or ZMQ acquire the data in any topic.

Patron (subscriber)

Creating a patron to subscribe to and consume information is also easy. It's similar to the brewer (publisher) side, except the brews are configured to receive information and create a patron object. In this example, we define our callback method, then subscribe to a given topic:

from pubkeeper.client import PubkeeperClient
from pubkeeper.patron import Patron
from pubkeeper.brew.websocket.brew import WebsocketBrew
from pubkeeper.brew.zmq.brew import ZMQBrew
import time


def on_message(data):
    print("{}".format(data))

config = {
    'host': 'localhost',
    'port': 9898,
    'secure': False
    'token': 'your-pk-token',
    'bridge_mode': False
}
client = PubkeeperClient(config)

websocket_brew = WebsocketBrew()
websocket_brew.configure({
    'ws_host': 'localhost',
    'ws_port': 9555,
    'ws_secure': False
})

zmq_brew = ZMQBrew()
zmq_brew.configure({})

client.add_brew(websocket_brew)
client.add_brew(zmq_brew)

client.start()

patron = client.add_patron('test.*.data', callback=on_message)

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    client.shutdown()

results matching ""

    No results matching ""