Skip to content

Instantly share code, notes, and snippets.

@igalshilman
Last active March 4, 2025 08:04
Show Gist options
  • Save igalshilman/9b037183c9a4f675551090f85a58245c to your computer and use it in GitHub Desktop.
Save igalshilman/9b037183c9a4f675551090f85a58245c to your computer and use it in GitHub Desktop.
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,
});
}
},
},
});
@igalshilman
Copy link
Author

you can even publish an event externally like this:

curl ${INGRESS_URL}/pubsub/emails/publish --json '{ "foo" : "bar" }'

@slinkydeveloper
Copy link

Awesome 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment