
Description
Asynchronous messaging in Encore.ts via `Topic` and `Subscription` from `encore.dev/pubsub` — broadcast events, decouple producers from consumers, and run background handlers.
SKILL.md
Encore Pub/Sub
Instructions
Pub/Sub is for asynchronous messaging between services. Producers publish events to a Topic; consumers attach Subscriptions to react. Resources must be declared at package level — never inside functions.
Topics
import { Topic } from "encore.dev/pubsub";
interface OrderCreatedEvent {
orderId: string;
userId: string;
total: number;
}
// Package level declaration
export const orderCreated = new Topic<OrderCreatedEvent>("order-created", {
deliveryGuarantee: "at-least-once",
});
Publishing
await orderCreated.publish({
orderId: "123",
userId: "user-456",
total: 99.99,
});
Subscriptions
import { Subscription } from "encore.dev/pubsub";
const _ = new Subscription(orderCreated, "send-confirmation-email", {
handler: async (event) => {
await sendEmail(event.userId, event.orderId);
},
});
Message Attributes
Use Attribute<T> for fields that should be treated as message attributes (for filtering/ordering):
import { Topic, Attribute } from "encore.dev/pubsub";
interface CartEvent {
cartId: Attribute<string>; // Used for ordering
userId: string;
action: "add" | "remove";
productId: string;
}
// Ordered topic: events with same cartId delivered in order
export const cartEvents = new Topic<CartEvent>("cart-events", {
deliveryGuarantee: "at-least-once",
orderingAttribute: "cartId",
});
Topic References
Pass topic access to other code while maintaining static analysis:
import { Publisher } from "encore.dev/pubsub";
const publisherRef = orderCreated.ref<Publisher>();
async function notifyOrder(ref: typeof publisherRef, orderId: string) {
await ref.publish({ orderId, userId: "123", total: 99.99 });
}
Delivery Guarantees
at-least-once(default): may deliver duplicates → handlers must be idempotent.exactly-once: stricter, capped throughput (AWS 300 msg/s/topic, GCP 3000+ msg/s/region). Does not deduplicate on the publish side.
Guidelines
- Topics and subscriptions must be declared at package level.
- Subscription handlers must be idempotent (at-least-once delivery is the default).
- Use
Attribute<T>for fields meant for filtering/ordering, not for arbitrary metadata. - Don't do heavy synchronous work in
publishcallers —publishreturns once the message is queued.
More skills from the skills repository
View all 28 skillsencore-api
build type-safe APIs with Encore
Apr 6API DevelopmentEncoreTypeScriptencore-auth
implement Encore.ts authentication
Apr 6AuthEncoreTypeScriptencore-bucket
store files in Encore.ts buckets
May 16BackendEncoreFile StorageFile Uploads +1encore-cache
cache data in Redis with Encore.ts
May 16BackendCachingEncoreRedis +1encore-code-review
review Encore.ts code
Apr 6Code ReviewEncoreTypeScriptencore-cron
schedule recurring jobs in Encore.ts
May 16AutomationBackendEncoreScheduling +1
More from Encore
View publisherencore-database
build Encore databases
skills
Apr 6DatabaseEncoreORMTypeScriptencore-frontend
connect frontend to Encore backend
skills
Apr 6EncoreFrontendNext.jsReactencore-getting-started
build and run applications with Encore.ts
skills
Apr 6API DevelopmentBackendEncoreLocal Development +2encore-go-api
build APIs with Encore Go
skills
Apr 6API DevelopmentEncoreGoencore-go-auth
implement authentication with Encore Go
skills
Apr 6AuthBackendEncoreGoencore-go-bucket
store files in Encore Go buckets
skills
May 16BackendEncoreFile StorageGo +1