Skip to content

Python SDK

The package is mesh_sdk in src/mesh/sdk. It is asynchronous and based on nats-py.

from mesh_sdk import Mesh, MeshError, NotAdvertised, Unreachable

Mesh(cluster_id, nats_url, bridge_url=None, bridge_token=None, **options)

options are passed to nats.connect, for example for credentials and TLS. Without a bridge, only local subjects can be used.

Method Purpose
advertise(subject, handler, *, inter_cluster=False, services=(), metadata=None, queue="") Serve a subject. Returns the subscription
withdraw(subject) Stop advertising it
send(subject, data, *, headers=None, timeout=30) Publish
send_recv(subject, data, *, headers=None, timeout=30) Request. Returns the reply
subscribe(subject, handler, *, queue="", timeout=30) Receive messages
recv(subject, *, timeout=30) Receive one message
close() Finish pending messages and disconnect

Mesh is an asynchronous context manager. Connections are cached and reconnect by themselves. For remote subjects, the first use waits up to timeout for the route.

handler(data, msg) receives the bytes and the NATS message with subject and headers.