Skip to main content

Module pubsub

Module pubsub 

Source
Expand description

Pub/Sub topics and subscriptions. Topics are typed by the message they carry and declared at module scope; a subscription names its topic and the function that handles its messages.

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 argument says how its messages are encoded: Json unless said otherwise, Raw for bytes passed through as they are, and — with the protobuf feature — Protobuf. It is part of the topic’s type, so a publisher and a subscriber cannot disagree about it, and Encore can read it from the declaration.

§Ordered topics

An OrderedTopic delivers the messages that share an ordering key in the order they were published. Its publish takes the key.

§Running subscriptions

A static does nothing until it is used, so something has to start a subscription. #[encore::pubsub::register] registers one to be started with the app, by crate::initialize or crate::run_blocking. One declared without it is started by hand, with Subscription::start.

§Tests

Under encore test each test publishes to topics of its own, and nothing handles what it publishes: no subscription is started. The test reads back what it — and the code it called — published with testing::published, and calls a handler itself when it wants one run.

Structs§

Message
A message, as a subscription’s handler receives it.
PublishOptions
Options for publishing a message.
Subscription
A subscription to a topic: every message published to the topic is handed to its handler.
Topic
A pub/sub topic carrying messages of type T, encoded as E.

Enums§

Json
JSON, for a message type that is Serialize and Deserialize. The default.
Ordered
Marks a Topic that orders messages by key: an OrderedTopic.
Raw
No encoding: the message is the bytes, as a Vec<u8>.
Unordered
Marks a Topic whose messages are delivered in no particular order.

Traits§

Encoding
How a topic’s messages are encoded. Implemented by the encodings, for the message types each can carry.

Type Aliases§

Handler
A subscription’s handler: a function returning the future that handles a message. An error has the message delivered again, as the retry policy says.
OrderedTopic
A Topic that delivers the messages sharing an ordering key in the order they were published.

Attribute Macros§

register
Registers a subscription to be started with the app.