topiq
Lightweight TCP-based publish/subscribe client library.
This crate re-exports the full consumer-facing API. It is the only dependency you need for client-only usage. Add the server feature to embed a broker in your application.
Add to your project
# Client only
= "0.1"
# Client + embedded broker
= { = "0.1", = ["server"] }
Quick start
use ;
async
Client is cheaply cloneable and safe to share across tasks.
Connecting
use ConnectOptions;
// Default: 127.0.0.1:4222
default
// Specific address
new
// Host string (DNS resolved at connect time)
from_host
// With tuning
from_host
.max_frame_size
.channel_buffer_size
Subjects and wildcards
Subjects are dot-separated tokens. Wildcards are only valid on the subscriber side:
// Exact match
client.subscribe.await?;
// * matches exactly one token
client.subscribe.await?;
// > matches one or more trailing tokens
client.subscribe.await?;
Publishing
The payload accepts anything that implements AsRef<[u8]> (&str, String, Vec<u8>, Bytes):
client.publish.await?;
client.publish.await?;
client.publish.await?;
Request / reply
use Duration;
// Caller
let reply = client.request.await?;
// Responder
let mut sub = client.subscribe.await?;
while let Some = sub.next_message.await
Queue groups
Multiple subscribers sharing a queue group receive messages round-robin, useful for load balancing workers:
let _w1 = client.subscribe_queue.await?;
let _w2 = client.subscribe_queue.await?;
Iterating a subscription with StreamExt
SubscriptionStream implements futures::Stream:
use StreamExt;
let mut sub = client.subscribe.await?;
while let Some = sub.next.await
Embedding a broker (server feature)
use Arc;
use ;
use ;
let shutdown = new;
let registry = new;
let router = new;
let listener = bind.await?;
let addr = listener.local_addr?;
spawn;
let client = connect.await?;
See the chat-server example for a full working demo.
License
MIT or Apache-2.0