{"library":"partyserver","title":"PartyServer for Cloudflare Durable Objects","description":"PartyServer is a TypeScript library for building real-time applications on Cloudflare Durable Objects, inspired by PartyKit. It extends Durable Objects with features like room-based routing, lifecycle hooks (`onConnect`, `onMessage`, `onClose`), a unified API for managing hibernated and non-hibernated Durable Objects, and easy broadcasting. Key differentiators from PartyKit include decoupling the URL from the server name, omitting built-in bindings for other Cloudflare services (encouraging `wrangler`'s native support), and requiring manual Durable Object bindings and migrations in `wrangler.jsonc`. The current stable version is 0.4.1, with frequent patch and minor releases, indicating active development. It is primarily designed for the Cloudflare Workers environment and leverages WebSockets for real-time communication.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install partyserver"],"cli":null},"imports":["import { Server } from 'partyserver';","import { routePartykitRequest } from 'partyserver';","import { PartySocket } from 'partysocket';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { routePartykitRequest, Server } from \"partyserver\";\nimport { PartySocket } from \"partysocket\";\n\ninterface Env {\n  MyServer: DurableObjectNamespace;\n}\n\n// Define your Server logic\nexport class MyServer extends Server {\n  onConnect(connection: WebSocket, context: any) {\n    console.log(`Connected ${connection.id} to server ${this.name}`);\n  }\n\n  onMessage(connection: WebSocket, message: string | ArrayBuffer) {\n    console.log(`Message from ${connection.id}:`, message);\n    // Broadcast the message to all other connections in the room\n    this.broadcast(message, [connection.id]);\n  }\n\n  onClose(connection: WebSocket, code: number, reason: string, wasClean: boolean) {\n    console.log(`Connection ${connection.id} closed. Clean: ${wasClean}`);\n  }\n}\n\n// Worker entry point to route requests to Durable Objects\nexport default {\n  async fetch(request: Request, env: Env): Promise<Response> {\n    return (\n      (await routePartykitRequest(request, env)) ||\n      new Response(\"Not Found\", { status: 404 })\n    );\n  }\n} satisfies ExportedHandler<Env>;\n\n// === Minimal wrangler.jsonc configuration ===\n/*\n{\n  \"name\": \"my-partyserver-app\",\n  \"main\": \"index.ts\",\n  \"durable_objects\": {\n    \"bindings\": [\n      {\n        \"name\": \"MyServer\",\n        \"class_name\": \"MyServer\"\n      }\n    ]\n  },\n  \"migrations\": [\n    {\n      \"tag\": \"v1\",\n      \"new_sqlite_classes\": [\"MyServer\"]\n    }\n  ]\n}\n*/\n\n// === Client-side connection example (requires 'partysocket') ===\n/*\nconst socket = new PartySocket({\n  host: \"https://my-partyserver-app.threepointone.workers.dev\",\n  party: \"my-server\", // 'my-server' corresponds to the kebab-cased Durable Object class name 'MyServer'\n  room: \"my-room\",\n  onOpen: () => console.log(\"Client connected!\"),\n  onMessage: (event) => console.log(\"Client received message:\", event.data),\n  onClose: () => console.log(\"Client disconnected\"),\n  onError: (event) => console.error(\"Client error:\", event)\n});\n\n// To send a message from the client\n// socket.send(\"Hello from client!\");\n*/\n","lang":"typescript","description":"Demonstrates a basic PartyServer setup with a Durable Object and its lifecycle hooks, along with the necessary `wrangler.jsonc` configuration and a client-side `PartySocket` example.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}