PartySocket
is a TypeScript client library for connecting to PartyKit servers via WebSockets.
Install
Features
- WebSocket API compatible (same interface, Level0 and Level2 event model)
- Automatically reconnect if the connection is closed.
- Multi-platform (Web, ServiceWorkers, Node.js, React Native)
- Dependency free (does not depend on Window, DOM or any EventEmitter library)
- Handle connection timeouts
- Allows changing server URL between reconnections
- Buffering. Will send accumulated messages on open
- Multiple builds available (see dist folder)
- Debug mode
- Fully configurable
- Works everywhere, not just with PartyKit!
Usage
Compatible with WebSocket Browser API
MDN WebSocket API.
Usage with PartyKit
Usage with React
PartySocket also exports a React hook for convenience:
The usePartySocket
hook handles connecting on mount, disconnecting on unmount, and cleaning up the on(Connect|Message|Close|Error)
handlers.
Usage with other WebSocket servers
PartySocket can be used with any WebSocket server, not only PartyKit servers.
Update URL
The url
parameter will be resolved before connecting, possible types:
string
() => string
() => Promise<string>
Update Protocols
The protocols
parameter will be resolved before connecting, possible types:
null
string
string[]
() => string | string[] | null
() => Promise<string | string[] | null>
Options
Sample with custom options
Available options
Default values
API
Methods
Attributes
More info
Constants
Acknowledgements