Skip to content

Context

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

Context (ctx) persists for the lifetime of a WebSocket connection.

const zo = zocket.create({
headers: z.object({
authorization: z.string().optional(),
}),
onConnect: async (headers, clientId) => {
const user = headers.authorization
? await verifyToken(headers.authorization)
: null;
return { user, connectedAt: new Date() };
},
});
PropertyTypeDescription
clientIdstringUnique identifier for the connection
roomsRoomOperationsHelper to join/leave rooms
  • join(roomId) — Add the connection to a room
  • leave(roomId) — Remove from a room
  • has(roomId) — Check membership
  • currentReadonlySet<string> of joined rooms