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

Next steps