Skip to content

Write a party ​

A party is a TypeScript class that handles connections to one room. synkd loads it from synkd.json and serves it at /parties/:party/:room.

Create a party class ​

ts
import { Server, type Connection } from "@synkd/server";

export class Chat extends Server {
  static options = { hibernate: true };

  onConnect(conn: Connection) {
    conn.send(JSON.stringify({ type: "hello", id: conn.id }));
  }

  onMessage(conn: Connection, message: string | ArrayBuffer) {
    this.broadcast(message, [conn.id]);
  }
}

Register the export in synkd.json:

json
{
  "parties": {
    "main": {
      "entry": "./src/chat.ts",
      "export": "Chat",
      "className": "ChatDO"
    }
  }
}

className is used when you deploy to a Durable Objects fleet (class name binding).

Room URL ​

Clients connect to:

/parties/<party-key>/<room-id>

Example: /parties/main/lobby-42.

Useful Server methods ​

MethodUse
onConnectNew WebSocket joined
onMessageClient sent data
onCloseSocket left
broadcastSend to others in the room
this.storagePer-room durable key/value
this.setAlarm / onAlarmDelayed work in the room

Enable idle unload with static options = { hibernate: true } when the party can safely wake from storage.

Hot reload ​

synkd up (and pnpm up in this repo) watches party source files and reloads them. Active sockets close with code 1012 so clients can reconnect.

Disable watching with --no-watch if you need a fixed process for debugging.

Start from a new project ​

Use the CLI to scaffold a Chat party, then edit src/chat.ts:

sh
pnpm --filter @synkd/cli exec tsx src/bin.ts init ./my-rooms --name my-rooms
pnpm --filter @synkd/cli exec tsx src/bin.ts dev --root ./my-rooms

See CLI for linking, tokens, and deploy.

What to do next ​

PartyKit-compatible rooms on infrastructure you run.