react-websocket 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -0
- package/README.md +8 -4
- package/dist/core.cjs +1013 -759
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +525 -374
- package/dist/core.d.ts +525 -374
- package/dist/core.js +1006 -727
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +217 -151
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +585 -410
- package/dist/index.d.ts +423 -524
- package/dist/index.js +204 -121
- package/dist/index.js.map +1 -1
- package/package.json +43 -28
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# react-websocket
|
|
2
2
|
|
|
3
|
-
**Hooks-first WebSocket client for React 18 and 19.** Shared connections, automatic reconnects, a send queue, typed and validated messages, and Suspense, in about
|
|
3
|
+
**Hooks-first WebSocket client for React 18 and 19.** Shared connections, automatic reconnects, a send queue, typed and validated messages, and Suspense, in about 3 kB (min+gzip) with zero dependencies.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/react-websocket)
|
|
6
6
|
[](https://github.com/mehmetkose/react-websocket/actions/workflows/ci.yml)
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
- **Topic subscriptions.** Reference-counted `subscribe`/`unsubscribe` messages that are sent again after every reconnect, so feeds don't silently stop.
|
|
20
20
|
- **Message state.** Reduce messages into a store, and let each component re-render only when its slice changes.
|
|
21
21
|
- **Requests and latency.** Await replies with timeouts and cancellation, and measure round-trip time and the server's clock for ping displays and synchronized countdowns.
|
|
22
|
+
- **Server requests.** Answer requests from the server, such as an AI agent's, in code for browser-only tools, or from a dialog for human approval. Every request gets exactly one reply, and none is sent to the wrong connection.
|
|
22
23
|
- **SSR-safe.** Never connects during a server render, hydrates cleanly, and ships a `'use client'` entry for Next.js and other Server Components frameworks.
|
|
23
24
|
- **Small and typed.** Zero dependencies, ESM + CJS, TypeScript declarations with inline docs, plus a React-free `react-websocket/core` entry.
|
|
24
25
|
|
|
@@ -114,11 +115,13 @@ function Room() {
|
|
|
114
115
|
| `useWebSocketTopic(topics, topic)` | Hold topic subscriptions that survive reconnects |
|
|
115
116
|
| `useWebSocketState(store, selector?)` | Read state reduced from messages; re-renders only when the selection changes |
|
|
116
117
|
| `useWebSocketLatency(latency)` | Round-trip time and server clock offset |
|
|
118
|
+
| `useAgentApproval(responder, method)` | Requests from the server waiting for a person to answer |
|
|
117
119
|
| `createWebSocket(url, options?)` | A shareable client: `send`, `on`, `connect`, `close`, `reconnect`, `ready`, `getSnapshot`, `subscribe` |
|
|
118
120
|
| `createWebSocketTopics(client, { subscribe, unsubscribe })` | Topic subscription manager for `useWebSocketTopic` |
|
|
119
121
|
| `createWebSocketState(client, { initialState, reduce })` | Message-state store for `useWebSocketState` |
|
|
120
122
|
| `createWebSocketRequests(client, { send, reply })` | `request(message)` returns a promise of the reply, with timeouts |
|
|
121
123
|
| `createWebSocketLatency(client, { ping, pong })` | Ping tracker for `useWebSocketLatency`, plus `serverNow()` |
|
|
124
|
+
| `createWebSocketResponder(client, { request, respond, error })` | Answer the server's requests: `onRequest(method, handler)`, or a person through `useAgentApproval` |
|
|
122
125
|
| `MessageError` | Reported to `onError` when a message fails `parse` or `schema` |
|
|
123
126
|
|
|
124
127
|
| Option | Default | |
|
|
@@ -140,7 +143,7 @@ function Room() {
|
|
|
140
143
|
|
|
141
144
|
- [Shared clients](https://mehmetkose.github.io/react-websocket/guide/shared-clients) · [Messages and types](https://mehmetkose.github.io/react-websocket/guide/messages) · [Reconnecting and heartbeats](https://mehmetkose.github.io/react-websocket/guide/reconnecting)
|
|
142
145
|
- [Suspense](https://mehmetkose.github.io/react-websocket/guide/suspense) · [Server rendering and Next.js](https://mehmetkose.github.io/react-websocket/guide/ssr) · [StrictMode, Activity and lifecycle](https://mehmetkose.github.io/react-websocket/guide/lifecycle)
|
|
143
|
-
- [Authentication](https://mehmetkose.github.io/react-websocket/guide/authentication) · [Performance](https://mehmetkose.github.io/react-websocket/guide/performance) · [Real-time games](https://mehmetkose.github.io/react-websocket/guide/games) · [Testing](https://mehmetkose.github.io/react-websocket/guide/testing) · [Without React](https://mehmetkose.github.io/react-websocket/guide/without-react)
|
|
146
|
+
- [Authentication](https://mehmetkose.github.io/react-websocket/guide/authentication) · [Performance](https://mehmetkose.github.io/react-websocket/guide/performance) · [Real-time games](https://mehmetkose.github.io/react-websocket/guide/games) · [AI agents](https://mehmetkose.github.io/react-websocket/guide/agents) · [Testing](https://mehmetkose.github.io/react-websocket/guide/testing) · [Without React](https://mehmetkose.github.io/react-websocket/guide/without-react)
|
|
144
147
|
- [Recipes](https://mehmetkose.github.io/react-websocket/recipes) · [Troubleshooting](https://mehmetkose.github.io/react-websocket/troubleshooting) · [Migrating from v2](https://mehmetkose.github.io/react-websocket/migration)
|
|
145
148
|
|
|
146
149
|
The docs source lives in [`docs/`](docs). Run `npm run docs:dev` to browse it locally.
|
|
@@ -157,9 +160,10 @@ The docs source lives in [`docs/`](docs). Run `npm run docs:dev` to browse it lo
|
|
|
157
160
|
|
|
158
161
|
v2 was a `<Websocket>` class component. v3 replaces it with hooks: `<Websocket url onMessage />` becomes `useWebSocket(url, { onMessage })`, and `ref.sendMessage(msg)` becomes `send(msg)`. See the [migration guide](https://mehmetkose.github.io/react-websocket/migration) for the full mapping.
|
|
159
162
|
|
|
160
|
-
##
|
|
163
|
+
## Examples
|
|
161
164
|
|
|
162
|
-
[`
|
|
165
|
+
- [`examples/nextjs`](examples/nextjs) is a demo of every feature in a Next.js App Router app, with its own WebSocket server. Run `npm run example:nextjs` and open http://localhost:3000.
|
|
166
|
+
- [`examples/tornado`](examples/tornado) is a small broadcast chat: a Tornado server and a Vite + React 19 client.
|
|
163
167
|
|
|
164
168
|
## Contributing
|
|
165
169
|
|