Skip to main content

Module pubsub

Module pubsub 

Source
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§

perms
Permission marker types for TopicRef. Each permission corresponds to a trait in pubsub.

Structs§

BatchError
Error returned when one or more messages in a publish_batch call could not be published.
BatchMessage
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.
OrderedBatchMessage
A single message in a batch passed to OrderedTopic::publish_batch, containing the payload, ordering key, and optional attributes.
PublishOptions
Options for publishing a single message.
Subscription
A subscription that delivers messages from a Topic to a handler function.
Topic
A Pub/Sub topic carrying messages of type T with encoding E.
TopicRef
A copyable, permission-scoped reference to a Topic, created by Topic::reference.

Enums§

Json
JSON encoding for message types implementing Serialize and Deserialize (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 reads Message::raw_data how to read the bytes. A publisher that sets it in its attributes keeps its own value, to name the format of a Raw payload, say.
MAX_BATCH_SIZE
Maximum number of messages accepted by a single publish_batch call.

Traits§

Encoding
Wire encoding for a topic’s messages.
OrderedPublisher
Trait for publishing messages of type T with ordering keys to an ordered topic.
Publisher
Trait for publishing messages of type T to an unordered topic.
TopicPerms
Sealed trait implemented by topic permission markers in perms.

Type Aliases§

Handler
Async handler function signature for a Subscription.
OrderedTopic
A Topic that delivers messages sharing an ordering key in publish order.
OrderedTopicRef
A permission-scoped reference to an OrderedTopic.

Attribute Macros§

register
Registers a subscription static to be started automatically when the application initializes.