Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/cloudflare/partykit/llms.txt

Use this file to discover all available pages before exploring further.

hono-party is a Hono middleware that integrates PartyServer’s Durable Object–backed WebSocket routing directly into any Hono application. You can combine REST endpoints, static file serving, and real-time multiplayer rooms all in a single Worker — no separate service needed.

Installation

npm install hono-party hono partyserver

Basic setup

The minimum configuration is two lines: import partyserverMiddleware and attach it with app.use('*', ...). Any request that matches a /parties/:server/:room pattern will be routed to the appropriate Durable Object; all other requests fall through to your normal Hono routes.
import { Hono } from "hono";
import { partyserverMiddleware } from "hono-party";
import { Server } from "partyserver";

export class Chat extends Server {}

const app = new Hono();
app.use("*", partyserverMiddleware());

export default app;

Authentication with Hono context

onBeforeConnect receives the raw Request, a lobby object, and the Hono context c, which gives you access to typed environment bindings. Return a Response to reject the connection before the WebSocket handshake completes.
import { Hono } from "hono";
import { partyserverMiddleware } from "hono-party";
import { Server } from "partyserver";

export class Chat extends Server {}

type Env = { Bindings: { JWT_SECRET: string } };

const app = new Hono<Env>();

app.use(
  "*",
  partyserverMiddleware<Env>({
    options: {
      onBeforeConnect: async (req, lobby, c) => {
        const token = req.headers.get("authorization");
        const secret = c.env.JWT_SECRET;
        // validate token against secret
        if (!token) return new Response("Unauthorized", { status: 401 });
      }
    }
  })
);

export default app;

Error handling

Pass an onError callback to log or report errors that occur inside the middleware.
app.use(
  "*",
  partyserverMiddleware({ onError: (error) => console.error(error) })
);

Custom routing prefix

By default hono-party handles the /parties/* path. Use the prefix option to scope it to a different base path.
app.use(
  "*",
  partyserverMiddleware({
    options: {
      prefix: "/party" // handles /party/* routes only
    }
  })
);

Multiple party servers

You can export as many Server subclasses as you need. Each class maps to its own Durable Object namespace — clients connect to party: "chat", party: "game", or party: "document" respectively.
import { Hono } from "hono";
import { partyserverMiddleware } from "hono-party";
import { Server } from "partyserver";

// Multiple party servers in one Worker
export class Chat extends Server {}
export class Game extends Server {}
export class Document extends Server {}

const app = new Hono();
app.use("*", partyserverMiddleware());

export default app;

wrangler.jsonc configuration

Each exported Server class needs a corresponding Durable Object binding and a migration entry in your wrangler.jsonc:
{
  "durable_objects": {
    "bindings": [
      { "name": "Chat", "class_name": "Chat" },
      { "name": "Game", "class_name": "Game" },
      { "name": "Document", "class_name": "Document" }
    ]
  },
  "migrations": [
    { "tag": "v1", "new_sqlite_classes": ["Chat", "Game", "Document"] }
  ]
}

React client

Connect from a React component using usePartySocket from partysocket/react. The party field maps to the kebab-cased class name of the Durable Object.
import { usePartySocket } from "partysocket/react";

function ChatRoom() {
  // connects to the Chat Durable Object, room "general"
  const socket = usePartySocket({ party: "chat", room: "general" });

  // connects to the Game Durable Object
  const gameSocket = usePartySocket({ party: "game", room: "uuid" });

  // connects to the Document Durable Object
  const docSocket = usePartySocket({ party: "document", room: "id" });
}

With authentication headers

const socket = usePartySocket({
  party: "chat",
  room: "general",
  headers: { authorization: `Bearer ${token}` }
});
hono-party was built by Thomas Osmonson. The middleware is a thin wrapper around routePartykitRequest — all PartyServer lifecycle hooks (onConnect, onMessage, onClose, etc.) work exactly as documented in the core partyserver package.

Build docs developers (and LLMs) love