Type-safe WebSocket library with Zod validation for Elysia and browser clients.
bun add @mdrv/wsx zod cbor-x elysia
// Define events
import { defineEvents } from '@mdrv/wsx/shared'
import { z } from 'zod'
export const events = defineEvents({
ping: {
request: z.object({ timestamp: z.number() }),
response: z.object({ pong: z.string() }),
},
})
// Server
import { createElysiaWS } from '@mdrv/wsx/server'
import { Elysia } from 'elysia'
const { server, handler } = createElysiaWS(events)
server.onRequest('ping', async payload => ({ pong: 'Hello!' }))
new Elysia().ws('/ws', handler).listen(3000)
// Client
import { createClient } from '@mdrv/wsx/client'
const client = createClient('ws://localhost:3000/ws', events)
client.connect()
client.onOpen(async () => {
const result = await client.request('ping', { timestamp: Date.now() })
console.log(result.pong) // "Hello!"
})
# Install dependencies
bun install
# Run tests
bun test
# Build package
bun b
# Build documentation
cd docs && bun b
MIT