EventPublisher
utility
oRPC includes a built-in EventPublisher
for real-time features like chat, notifications, or live updates. It supports broadcasting and subscribing to named events. docs
import { EventPublisher } from '@orpc/server'
const publisher = new EventPublisher<Record<string, { message: string }>>()
const onMessage = os
.input(z.object({ channel: z.string() }))
.handler(async function* ({ input, signal }) {
for await (const payload of publisher.subscribe(input.channel, { signal })) {
yield payload.message
}
})
const sendMessage = os
.input(z.object({ channel: z.string(), message: z.string() }))
.handler(({ input }) => {
publisher.publish(input.channel, { message: input.message })
})