Last active
March 4, 2025 08:04
-
-
Save igalshilman/9b037183c9a4f675551090f85a58245c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Subscriber = { | |
service: string; | |
handler: string; | |
key?: string; | |
}; | |
export const pubsub = restate.object({ | |
name: "pubsub", | |
handlers: { | |
/** | |
* Add a subscriber to the list of subscribers | |
*/ | |
subscribe: async (ctx: restate.ObjectContext, subscriber: Subscriber) => { | |
const current = (await ctx.get<Subscriber[]>("sub")) ?? []; | |
current.push(subscriber); | |
ctx.set("sub", current); | |
}, | |
/** | |
* Remove a subscriber | |
* | |
*/ | |
unsubscribe: async (ctx: restate.ObjectContext, subscriber: Subscriber) => { | |
const current = (await ctx.get<Subscriber[]>("sub")) ?? []; | |
const filtered = current.filter((s) => s !== subscriber); | |
ctx.set("sub", filtered); | |
}, | |
/** | |
* publish a message | |
*/ | |
publish: async (ctx: restate.ObjectContext, data: any) => { | |
const current = (await ctx.get<Subscriber[]>("sub")) ?? []; | |
for (const subscriber of current) { | |
ctx.genericSend({ | |
service: subscriber.service, | |
method: subscriber.handler, | |
key: subscriber.key, | |
parameter: data, | |
inputSerde: restate.serde.json, | |
}); | |
} | |
}, | |
}, | |
}); |
Author
igalshilman
commented
Mar 3, 2025
•
you can even publish an event externally like this:
curl ${INGRESS_URL}/pubsub/emails/publish --json '{ "foo" : "bar" }'
Awesome 😍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment