Define Events

In pluv.io, you can define custom events with an input validation schema and resolver, and have them be automatically type-safe without having to manage your own types.

Usage example

1import { createIO } from "@pluv/io";
2import { platformNode } from "@pluv/platform-node";
3import { z } from "zod";
4
5export const io = createIO({ platform: platformNode() })
6 // When event "SEND_MESSAGE" is sent by the frontend and received
7 // on the server
8 .event("SEND_MESSAGE", {
9 // Define a zod validation schema for the input
10 input: z.object({
11 message: z.string(),
12 }),
13 // Emit a "MESSAGE_RECEIVED" from the server to the client
14 resolver: ({ message }) => ({ MESSAGE_RECEIVED: { message } }),
15 })
16 .event("EMIT_EMOJI", {
17 input: z.object({
18 emojiCode: z.number(),
19 }),
20 resolver: ({ emojiCode }) => ({ EMOJI_RECEIVED: { emojiCode } }),
21 });
22
23// Export the io type instance of the io itself
24export type AppPluvIO = typeof io;

Next steps