Guides

Sockets

defineSocket files under sockets/ serve WebSockets on the project host.

Default-export defineSocket from robodev/sockets/<name>.ts. sockets/chat.ts serves wss://{projectHost}/sockets/chat. Bracket segments become path params: sockets/rooms/[roomId].ts serves /sockets/rooms/:roomId and sets ctx.params.roomId. robodev dev serves the same handlers on ws://localhost:{port}/sockets/…. A successful reload closes open sockets with 1012 deployed so clients reconnect; a failed compile keeps the previous sockets. This is not the control-plane Build WebSocket and not Postgres CDC.

auth defaults to false (public). Use "required" or "optional" for Robodev Auth. Browsers cannot set Authorization on WebSocket; pass ?accessToken= or a Bearer header. Same tokens as HTTP. Messages are text JSON up to 100KB. Invalid JSON closes with 1007. Binary is out of scope. At most 50 connections per project.

onConnect, onMessage, and onClose run with no time limit unless timeoutMs is set. An explicit timeoutMs is a positive integer used as written (not clamped to 30s). The connection stays open with no max lifetime. The server pings every 60s; a peer that does not pong by the next ping is closed with 1008 idle. A deploy or robodev dev reload closes open sockets with 1012 deployed. An open socket keeps a live hosted worker from idle-stopping; it does not start a worker by itself.

ctx.room.join / leave / broadcast are in-process rooms scoped to that socket definition. broadcast sends to other members of the room. APIs and jobs get ctx.sockets.send({ name, room?, payload }) and ctx.sockets.broadcast({ name, payload }). Unknown socket names throw unknown_socket. A missing room is a no-op for send with room. Socket handlers also get ctx.llm, ctx.agent, and ctx.pdf — stream tokens with llm.stream; see LLM (/docs/llm). Tenant agent sessions use ctx.agent; see Agents (/docs/agents). HTML to PDF uses ctx.pdf.fromHtml; see PDF (/docs/pdf).

In the browser, createClient({ url }).socket("/sockets/chat") returns a WebSocket to the project host and attaches accessToken when a stored token exists (http(s) → ws(s)).

robodev/sockets/chat.ts

import { defineSocket } from "@robodev-ai/sdk";

export default defineSocket({
  auth: "optional",
  onConnect: async ({ room }) => {
    room.join("lobby");
  },
  onMessage: async ({ send, room, user }, data) => {
    room.broadcast("lobby", { from: user?.email ?? "anon", data });
    send({ ok: true });
  },
});