encore-pubsub
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEncore Pub/Sub
Encore Pub/Sub
Instructions
使用说明
Pub/Sub is for asynchronous messaging between services. Producers publish events to a ; consumers attach s to react. Resources must be declared at package level — never inside functions.
TopicSubscriptionPub/Sub用于服务间的异步消息传递。生产者将事件发布到;消费者通过附加来响应事件。资源必须在包级别声明——绝不能在函数内部声明。
TopicSubscriptionTopics
主题(Topics)
typescript
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",
});typescript
import { Topic } from "encore.dev/pubsub";
interface OrderCreatedEvent {
orderId: string;
userId: string;
total: number;
}
// 包级别声明
export const orderCreated = new Topic<OrderCreatedEvent>("order-created", {
deliveryGuarantee: "at-least-once",
});Publishing
发布消息(Publishing)
typescript
await orderCreated.publish({
orderId: "123",
userId: "user-456",
total: 99.99,
});typescript
await orderCreated.publish({
orderId: "123",
userId: "user-456",
total: 99.99,
});Subscriptions
订阅(Subscriptions)
typescript
import { Subscription } from "encore.dev/pubsub";
const _ = new Subscription(orderCreated, "send-confirmation-email", {
handler: async (event) => {
await sendEmail(event.userId, event.orderId);
},
});typescript
import { Subscription } from "encore.dev/pubsub";
const _ = new Subscription(orderCreated, "send-confirmation-email", {
handler: async (event) => {
await sendEmail(event.userId, event.orderId);
},
});Message Attributes
消息属性(Message Attributes)
Use for fields that should be treated as message attributes (for filtering/ordering):
Attribute<T>typescript
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",
});使用标记应被视为消息属性的字段(用于过滤/排序):
Attribute<T>typescript
import { Topic, Attribute } from "encore.dev/pubsub";
interface CartEvent {
cartId: Attribute<string>; // 用于排序
userId: string;
action: "add" | "remove";
productId: string;
}
// 有序主题:具有相同cartId的事件按顺序传递
export const cartEvents = new Topic<CartEvent>("cart-events", {
deliveryGuarantee: "at-least-once",
orderingAttribute: "cartId",
});Topic References
主题引用(Topic References)
Pass topic access to other code while maintaining static analysis:
typescript
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 });
}在保持静态分析能力的同时,将主题访问权限传递给其他代码:
typescript
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
交付保障(Delivery Guarantees)
- (default): may deliver duplicates → handlers must be idempotent.
at-least-once - : stricter, capped throughput (AWS 300 msg/s/topic, GCP 3000+ msg/s/region). Does not deduplicate on the publish side.
exactly-once
- (默认):可能会传递重复消息 → 处理器必须具备幂等性。
at-least-once - :保障更严格,但吞吐量受限(AWS 300条消息/秒/主题,GCP 3000+条消息/秒/区域)。不会在发布端进行去重。
exactly-once
Guidelines
指导原则(Guidelines)
- Topics and subscriptions must be declared at package level.
- Subscription handlers must be idempotent (at-least-once delivery is the default).
- Use for fields meant for filtering/ordering, not for arbitrary metadata.
Attribute<T> - Don't do heavy synchronous work in callers —
publishreturns once the message is queued.publish
- 主题和订阅必须在包级别声明。
- 订阅处理器必须具备幂等性(默认交付保障为至少一次)。
- 仅对用于过滤/排序的字段使用,不要用于任意元数据。
Attribute<T> - 不要在调用方中执行繁重的同步工作——
publish在消息入队后立即返回。publish