Publish an event
Send event data from your application code.
Publish an event from the function that performs the operation, so other functions can react to its result.
Publish from a function
First, define orders.created. Then publish it from the defineFunction that creates the order:
import { defineFunction } from "@relkit/app/functions";
import { z } from "@relkit/app/schema";
const createOrder = defineFunction({
input: z.object({
orderId: z.string().min(1),
sku: z.string().min(1),
quantity: z.number().int().positive(),
}),
output: z.object({
orderId: z.string(),
sku: z.string(),
totalCents: z.number().int().nonnegative(),
}),
publishes: ["orders.created"],
handler: async (input, context) => {
const order = { orderId: input.orderId, sku: input.sku, totalCents: input.quantity * 100 };
await context.events["orders.created"].publish(order);
return order;
},
});
export default createOrder;
This function uses the Orders example’s priceOrder helper. To publish the event:
- Add
publishes: ["orders.created"]to allow this function to publish the event. - Call
await context.events["orders.created"].publish(order)to send the order data.
Use the exact event ID in both places. The data must match the event’s input schema. Only events listed in publishes are available through context.events.
Publishing waits for the event to be accepted, not for its consumers to finish. Your function can return the order while confirmation runs separately.
Publish from an event function
A defineEventFunction can publish events the same way: list each ID in publishes, then call .publish().