Expand description
Pub/Sub topics and subscriptions.
Declare topics and subscriptions as module-level statics. Topics are typed
by their message payload, and subscriptions bind a topic to an async handler
function:
use encore::pubsub::{Message, Subscription, Topic};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct SignupEvent {
user_id: String,
}
static SIGNUPS: Topic<SignupEvent> = Topic::new("signups");
#[encore::pubsub::register]
static WELCOME: Subscription<SignupEvent> =
Subscription::new(&SIGNUPS, "send-welcome", send_welcome);
async fn send_welcome(msg: Message<SignupEvent>) -> Result<(), encore::Error> {
println!("welcome, {}", msg.data.user_id);
Ok(())
}
async fn signed_up(user_id: String) -> Result<(), encore::Error> {
SIGNUPS.publish(&SignupEvent { user_id }).await?;
Ok(())
}§Encodings
A topic’s second type parameter specifies its wire encoding: Json by
default, Raw for raw byte payloads, or Protobuf when the protobuf
feature is enabled. Because the encoding is part of the topic’s type,
publishers and subscribers stay in agreement and Encore can read it
statically.
§Ordered topics
An OrderedTopic delivers messages sharing the same ordering key in the
order they were published. Pass the ordering key when calling publish.
§Batches
publish_batch publishes up to MAX_BATCH_SIZE messages in fewer
round-trips than individual publishes, returning message IDs in input order.
Batch publishing is not atomic: on partial failure, BatchError reports
which messages succeeded and which failed. On an ordered topic, messages
sharing a key are published in order, and if one fails, subsequent messages
with that key are not published.
§References
Encore infers service permissions from direct method calls on a Topic. To
pass a topic to helper functions, create a permission-scoped reference via
Topic::reference with perms::Publisher, and accept the Publisher
(or OrderedPublisher) trait in the helper.
§Starting subscriptions
Rust statics are lazy, so subscriptions must be started explicitly.
Annotate subscriptions with #[encore::pubsub::register]
so crate::initialize or crate::run_blocking starts them
automatically; otherwise call Subscription::start manually.
§Testing
Under encore test, each test has an isolated Pub/Sub scope and
subscriptions do not run automatically. Inspect messages published during a
test with testing::published, and invoke
subscription handlers explicitly with
testing::deliver.
Modules§
Structs§
- Batch
Error - Error returned when one or more messages in a
publish_batchcall could not be published. - Batch
Message - A single message in a batch passed to
Topic::publish_batch, containing the payload and optional attributes. - Message
- A Pub/Sub message delivered to a subscription handler.
- Ordered
Batch Message - A single message in a batch passed to
OrderedTopic::publish_batch, containing the payload, ordering key, and optional attributes. - Publish
Options - Options for publishing a single message.
- Subscription
- A subscription that delivers messages from a
Topicto a handler function. - Topic
- A Pub/Sub topic carrying messages of type
Twith encodingE. - Topic
Ref - A copyable, permission-scoped reference to a
Topic, created byTopic::reference.
Enums§
- Json
- JSON encoding for message types implementing
SerializeandDeserialize(the default topic encoding). - Ordered
- Marker type for an ordered
Topic(OrderedTopic). - Raw
- Raw byte encoding that passes
Vec<u8>payloads through unmodified. - Unordered
- Marker type for an unordered
Topic.
Constants§
- CONTENT_
TYPE_ ATTRIBUTE - The message attribute each published message carries its payload’s format
in: its topic’s
Encoding::CONTENT_TYPE. It tells a subscriber that readsMessage::raw_datahow to read the bytes. A publisher that sets it in its attributes keeps its own value, to name the format of aRawpayload, say. - MAX_
BATCH_ SIZE - Maximum number of messages accepted by a single
publish_batchcall.
Traits§
- Encoding
- Wire encoding for a topic’s messages.
- Ordered
Publisher - Trait for publishing messages of type
Twith ordering keys to an ordered topic. - Publisher
- Trait for publishing messages of type
Tto an unordered topic. - Topic
Perms - Sealed trait implemented by topic permission markers in
perms.
Type Aliases§
- Handler
- Async handler function signature for a
Subscription. - Ordered
Topic - A
Topicthat delivers messages sharing an ordering key in publish order. - Ordered
Topic Ref - A permission-scoped reference to an
OrderedTopic.
Attribute Macros§
- register
- Registers a subscription
staticto be started automatically when the application initializes.