You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/1.guide/3.peer.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,12 +60,40 @@ All topics, this peer has been subscribed to.
60
60
61
61
Peer's pubsub namespace.
62
62
63
+
### `peer.bufferedAmount`
64
+
65
+
Number of bytes queued for transmission but not yet flushed to the client.
66
+
67
+
Use this to apply **backpressure**: pause sending while it grows past a high watermark and resume once it drops. This prevents a fast producer (for example a streaming async generator) from filling the adapter's send buffer faster than the client can drain it. The easiest way to wait is [`peer.waitForDrain()`](#peerwaitfordrain).
68
+
69
+
> [!NOTE]
70
+
> Not all adapters expose a buffer signal; those return `0`. Refer to the [compatibility table](#compatibility) for more info.
Returns a promise that resolves once [`peer.bufferedAmount`](#peerbufferedamount) drops to or below `threshold` bytes (default `0`), letting you `await` backpressure relief in a send loop:
It resolves immediately when there is no backpressure (or on adapters that do not report `bufferedAmount`). Otherwise it polls every `pollInterval` ms (default `100`) until the buffer drains, and also resolves early if the connection is no longer open so a send loop never hangs on a dropped client. Pass a `signal` (e.g. `AbortSignal.timeout(ms)`) to abort the wait — the promise then rejects with the signal's reason.
93
+
94
+
> [!TIP]
95
+
> For event-driven backpressure (instead of `await`ing in a loop), use the [`drain` hook](/guide/hooks) together with `peer.bufferedAmount`.
96
+
69
97
### `peer.subscribe(channel)`
70
98
71
99
Join a broadcast channel.
@@ -113,6 +141,8 @@ To gracefully close the connection, use `peer.close()`.
@@ -145,3 +175,7 @@ To gracefully close the connection, use `peer.close()`.
145
175
[^6]: [`websocket.readyState`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState) is polyfilled by tracking open/close events.
146
176
147
177
[^7]: Some runtimes have non standard values including `"nodebuffer"` and `"uint8array"`. crossws auto converts them for [`message.data`](/guide/message#messagedata).
178
+
179
+
[^8]: The runtime exposes no send-buffer signal, so `peer.bufferedAmount` reports `0`. (Cloudflare buffers and applies backpressure internally; SSE has no equivalent.)
180
+
181
+
[^9]: The runtime emits no drain signal. Poll [`peer.bufferedAmount`](#peerbufferedamount) instead where it is available.
0 commit comments