Skip to main content

Python SDK for CloudPostOffice — messaging for AI agents, apps, and devices

Project description

cloudpostoffice

Python SDK for CloudPostOffice — super-simple messaging for AI agents, apps, and devices.

Install

pip install cloudpostoffice

Quick start

Each app or device needs a unique device ID and a secret key. Create them from your dashboard. Two apps cannot connect with the same device ID at the same time — every participant needs its own credentials.


Direct Messages

Send a message directly from one device to another.

import asyncio
import cloudpostoffice as cpo

d1 = cpo.device('device-1', 'your-secret')
d2 = cpo.device('device-2', 'your-secret')

async def main():
    def on_message(msg):
        print(msg)  # { 'from': 'device-1', 'msg': 'hello', 'ts': 1234567890 }

    # device-2 listens for incoming messages
    await d2.listen(on_message)

    # device-1 sends a message to device-2
    await d1.send(to='device-2', msg='hello')

asyncio.run(main())

The listen callback receives a dict { 'from', 'msg', 'ts' } where from is the sender's device ID, msg is the payload, and ts is the server timestamp.


Pub/Sub

Any device can publish or subscribe to any topic in the same project. No need to pre-create topics — they work on the fly.

import asyncio
import cloudpostoffice as cpo

d1 = cpo.device('device-1', 'your-secret')
d2 = cpo.device('device-2', 'your-secret')

async def main():
    def on_news(topic, msg):
        print(topic, msg)

    # d1 subscribes to a topic
    await d1.subscribe('news', on_news)

    # d2 publishes to the same topic
    await d2.publish('news', 'CloudPostOffice is alive!')

asyncio.run(main())

The subscribe callback receives (topic_name, message).


API

cpo.device(device_id, device_secret)

Creates a device handle. Automatically authenticates and connects to the MQTT broker on first use.

d = cpo.device('my-device', 'my-secret')

await device.send(to, msg)

Sends a direct message to another device on the same account/project.

Param Type Description
to str Target device ID
msg any Message payload (any JSON-serialisable value)
await d1.send(to='device-2', msg='hello')

await device.listen(callback)

Registers a callback for messages addressed to this device. Can be called multiple times to add multiple handlers.

def on_message(msg):
    print(f"Message from {msg['from']}:", msg['msg'])

await d.listen(on_message)

await device.publish(topic_name, message)

Publishes a message to a named topic.

  • Topic names must not contain /, +, #, or --.
await d.publish('alerts', {'level': 'warn', 'text': 'High temp'})

await device.subscribe(topic_name, callback)

Subscribes to a named topic. Callback is called whenever a message is published to that topic.

def on_alert(topic, msg):
    print(topic, msg)

await d.subscribe('alerts', on_alert)

device.disconnect()

Gracefully closes the MQTT connection.

d.disconnect()

Notes

  • Authentication tokens are valid for 7 days. The SDK will automatically reconnect and refresh the token when it expires.
  • Topic names must not contain /, +, #, or --.
  • Two devices cannot share the same device ID and secret at the same time within a project.

Links

Project details


Download files

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

Source Distribution

cloudpostoffice-1.2.1.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudpostoffice-1.2.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file cloudpostoffice-1.2.1.tar.gz.

File metadata

  • Download URL: cloudpostoffice-1.2.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cloudpostoffice-1.2.1.tar.gz
Algorithm Hash digest
SHA256 f33e4f58866f9a89028826773d6c61e60e6a866ff173128400297dded8165c09
MD5 45b7c40f0d66bf5ccaae7962888bcd9e
BLAKE2b-256 5cfbd65ee8933382df6c255da4eef153b45bf52cb7fec7b17192315bac2e420e

See more details on using hashes here.

File details

Details for the file cloudpostoffice-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cloudpostoffice-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3dba3e8be90cf2a3c59627b9b53438ecdce2b022cfcd7c55e65153a0dcf48fa5
MD5 5a56e96a97fec5c0602a303a90ac3819
BLAKE2b-256 d0e5a0b942eeaccae2edd7b9e68c3c216e70d2b99bf68acc123251450000b806

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page