Skip to content

Procedures

This documents the old v1 API. See Actors for the current version.

Procedures (incoming messages) are the client → server entry points in your Zocket router.

  1. Input Validation (Optional)
  2. Middleware (Optional)
  3. Handler (Required)
const sendMessage = zo.message
.use(authMiddleware)
.input(z.object({ text: z.string() }))
.handle(({ ctx, input }) => {
console.log(ctx.clientId, input.text);
});
.input(
z.object({
title: z.string().min(1),
tags: z.array(z.string()).max(5).optional()
})
)

If the input is invalid, the handler will not run.

Handlers can be asynchronous and return values (RPC style):

.handle(async ({ ctx, input }) => {
const post = await db.post.create({ data: { title: input.title } });
return post;
})