AC

Publishers & Subscribers

The pub/sub abstraction in Zenoh provides location-transparent data in motion.

Publishers

A publisher declares intent to produce data on a key expression:

let publisher = session
    .declare_publisher("robots/*/pose")
    .await
    .unwrap();
 
// Send data
publisher.put(payload_bytes).await.unwrap();
 
// With metadata
publisher
    .put(payload_bytes)
    .encoding(Encoding::APPLICATION_JSON)
    .await
    .unwrap();

Declaring a publisher (rather than using session.put() directly) allows the router to pre-compute routes, improving throughput.

Subscribers

let subscriber = session
    .declare_subscriber("robots/**")
    .await
    .unwrap();
 
// Async handler
while let Ok(sample) = subscriber.recv_async().await {
    println!("Key: {}", sample.key_expr());
}
 
// Callback handler
let _subscriber = session
    .declare_subscriber("robots/**")
    .callback(|sample| println!("Got: {}", sample.key_expr()))
    .await
    .unwrap();

Data Flow

The router matches each publication against all active subscriber key expressions and delivers only to those that intersect.

Pub/sub fan-out through the router

Sample

Every received message is a Sample containing:

FieldTypeDescription
key_expr()KeyExprThe exact key the data was published on
payload()ZBytesThe raw payload
encoding()EncodingPayload type hint
timestamp()Option<Timestamp>Optional HLC timestamp
kind()SampleKindPut or Delete

Delete

Publish a deletion (e.g., to remove a key from storages):

session.delete("robots/robot-42/pose").await.unwrap();