pubsub.client module

class pubsub.client.PubSubClient(base_url)

Bases: object

Python API client that wraps the PubSub WebSocket interface. You must connect to the server before you can start using the client:

client = PubSubClient('ws://localhost:8011')
await client.connect()
# ... do stuff with client ...
await client.close()

You can use a client as a context to automatically connect/close:

async with PubSubClient('ws://localhost:8011') as client:
    ... do stuff with client ...

See pub_example.py and sub_example.py for examples.

async close()

Closes connection to the WebSocket server.

async connect()

Connect to the WebSocket server. Returns self.

async incoming_messages()

Infinite generator that yields messages received on all currently subscribed topics. Example:

async for msg_ripc in client.incoming_messages():
    print(f"Received: {msg_ripc['topic']}: {msg_ripc['msg']}")
async subscribe(topic)

Subscribe to the specified topic. Messages published to this topic will be received when calling incoming_messages().

topic(topic)

Create a new PubSubTopic object, which can be used to make a PUBLISH request and to publish messages.

async unsubscribe(topic)

Unsubscribe from the specified topic.

class pubsub.client.PubSubTopic(client, topic)

Bases: object

Don’t create directly! Create with PubSubClient.topic(). You must register the topic before you send messages:

topic = client.topic('example topic')
await topic.register()
await topic.send('message one')
await topic.send('message 2')
await topic.deregister()

You can use a topic object as a context to automatically register/deregister:

async with client.topic('example topic') as topic:
    topic.send('message one')
    topic.send('message 2')

Can be used standalone or as an async context.

async deregister()

Submit an UNPUBLISH request for this topic.

async register()

Submit a PUBLISH request for this topic.

async send(msg)

Send a message on this topic.