partysocket
A drop-in, auto-reconnecting WebSocket client with message buffering and React hooks
Repository Health
Technical Analysis
PartySocket is a WebSocket client that implements the same interface as the browser’s native WebSocket API, but adds the resilience that real applications need: automatic reconnection with configurable backoff, buffering of outgoing messages while disconnected, connection-timeout handling, and the ability to change the server URL between reconnect attempts. It began as a fork of reconnecting-websocket, updated with pending fixes, then extended by the Cloudflare/PartyKit team with first-class helpers for connecting to PartyKit and PartyServer (Durable Object) rooms by host/room/party, plus React hooks for using it idiomatically inside components.
Because it has no dependency on window, the DOM, or a particular event-emitter library, it runs unmodified in browsers, Service Workers, Node.js, React Native, Cloudflare Workers, and Deno/Bun, making it a practical choice any time an app needs one reliable WebSocket client across multiple runtimes.
What You Get
- A
WebSocket-API-compatible client class that’s a drop-in replacement for the native constructor - Automatic reconnection with configurable min/max delay, backoff growth factor, and retry limits
- Outgoing message buffering while disconnected, flushed automatically once the connection reopens
- A
PartySocketsubclass with host/room/party/query URL construction for PartyKit and PartyServer rooms, plus a matchingPartySocket.fetchhelper - React hooks (
usePartySocket,useWebSocket) exposed via dedicatedpartysocket/reactandpartysocket/use-wsentry points - An
event-target-polyfillsub-export for environments missing a globalEventTarget/Event
Common Use Cases
- Adding resilient, auto-reconnecting WebSocket connections to a web or React Native app without hand-rolling backoff logic
- Connecting a frontend to Cloudflare Durable Object-backed realtime backends built with PartyKit or PartyServer
- Sharing one WebSocket client implementation across browser, Node.js, and edge/Worker code paths
Under The Hood
Architecture - The package layers three pieces on top of a core reconnecting-WebSocket implementation. src/ws.ts (722 lines) implements ReconnectingWebSocket: it wraps a real WebSocket constructor, tracks retry/backoff state, re-emits browser-standard open/close/error/message events, and includes separate event-cloning paths for browser vs. Node/React Native runtimes (cloneEventBrowser vs cloneEventNode) because native Event construction differs across them. src/index.ts layers PartySocket on top, translating host/room/party/query options into a ws:///wss:// URL (and a matching PartySocket.fetch for one-off HTTP calls to the same room) before delegating to the base class. src/react.ts, use-socket.ts, use-handlers.ts, and use-ws.ts add a React hook layer (usePartySocket, useWebSocket) that manages the socket’s lifecycle against component mount/unmount and re-renders. Tech Stack - Written entirely in TypeScript, built with tsdown into dual ESM/CJS output with generated .d.ts/.d.cts declarations, then reformatted with oxfmt. Linting is via oxlint, tests run under vitest, and the package lives in an npm-workspaces monorepo (cloudflare/partykit) versioned with Changesets. Runtime dependencies are minimal: only its own tiny event-target-polyfill package, with react as an optional peer dependency gated by peerDependenciesMeta. Code Quality - The package ships 13 test files under src/tests/ covering reconnection behavior, error handling, React Native compatibility, module-scope isolation, React hooks, React SSR, integration flows, edge cases, performance, and the fetch helper — unusually thorough for a client library of this size. Typing is strict throughout, with any usage limited to a handful of documented event-cloning casts flagged by inline oxlint-disable comments rather than silently suppressed. API Design - The headline design decision is drop-in compatibility with the native WebSocket constructor, so existing code can adopt it with a single import swap. On top of that it exposes purpose-built subpath exports (partysocket/ws, partysocket/react, partysocket/use-ws, partysocket/event-target-polyfill) so consumers only pull in the pieces they use, plus a small, well-documented Options object (backoff timings, retry caps, buffering limits) with sensible defaults that require zero configuration to get a working reconnecting socket.
Used by 10 apps in this directory
BrowserOS
Browser · AI Assistants
The open-source agentic Chromium browser with native AI agents, MCP server, and visual workflow automation — your data never leaves your machine.
Enso
Analytics · Data Engineering · Low Code Platforms
A visual and textual programming platform for data prep and analysis where the node graph and the underlying Enso code are always perfectly in sync, built by an Alteryx co-founder on a GraalVM engine.
marimo
Developer Tools · Data Engineering
A reactive Python notebook that eliminates hidden state, runs reproducibly, and deploys as a web app or script — stored as pure Python, built for the AI era.
Novu
Developer Tools
Open-source communication infrastructure that connects your products and AI agents to every channel your users live on — Inbox, Email, SMS, Push, Chat, and more.
Openship
Devops · Hosting Control Panel
Openship is an open-source, self-hostable deployment platform that points at a repo and builds, ships, routes, and TLS-terminates the app — driven from a desktop app, web dashboard, or CLI.
superset
AI Code Assistants · AI Development
Orchestrate an army of AI coding agents—Claude Code, Codex, Gemini CLI, and more—running simultaneously in isolated git worktrees from a single Electron desktop app.
Trigger.dev
Automation · AI Development · Developer Tools
Build and deploy fully-managed AI agents and background jobs in TypeScript — with no timeouts, durable retries, real-time observability, and elastic scaling built in.
Typebot
Developer Tools · AI Development · No Code Platforms
Build sophisticated chatbots visually, embed them anywhere without iframes, and own your data — fully self-hostable with a modular block system and 30+ integrations.
Webstudio
Design Tools · No Code Platforms
Open source visual development platform with complete CSS control, headless CMS integration, and full infrastructure ownership