From b58eba86775043f0e37195c32379475e043a7159 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Mon, 23 Mar 2026 01:59:45 +0100 Subject: [PATCH 1/2] Add Peer.maxMessageSize() maxMessageSize() returns the max bytes per data channel message for this peer. This is useful if you want to transfer as much data as fast as possible for example. --- docs/api-reference.md | 12 ++++++++++++ docs/basic-usage.md | 18 ++++++++++++++++++ lib/peer.ts | 4 ++++ 3 files changed, 34 insertions(+) diff --git a/docs/api-reference.md b/docs/api-reference.md index e53081d..a0980e0 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -96,6 +96,7 @@ Subscribe to events using `network.on(eventName, callback)`: ```typescript interface Peer { id: string; // Unique peer identifier + maxMessageSize: number | null; // Max bytes per data channel message for this peer latency?: { last: number; // Most recent latency measurement (in ms) average: number; // Average latency over time @@ -105,3 +106,14 @@ interface Peer { }; } ``` + +##### `peer.maxMessageSize: number | null` (read-only) +Maximum message size, in bytes, that can be sent in a single WebRTC data channel message to this peer. + +- `null`: the SCTP transport is not available yet or unsupported in the current environment. +- `0`: no practical message size limit is reported by the transport. +- `> 0`: maximum safe payload size in bytes. + +Notes: +- This value is negotiated per peer and can differ across connections. +- Use this to decide when to chunk large payloads. diff --git a/docs/basic-usage.md b/docs/basic-usage.md index 5338ad9..c71c525 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -100,6 +100,24 @@ network.on('disconnected', peer => { }) ``` +##### Inspecting Message Size Limits +```js +network.on('connected', peer => { + const limit = peer.maxMessageSize + if (limit === null) { + console.log(`No SCTP max message size available for ${peer.id} yet`) + return + } + + if (limit === 0) { + console.log(`No practical message size limit reported for ${peer.id}`) + return + } + + console.log(`Max payload for ${peer.id}: ${limit} bytes`) +}) +``` + #### 6. Listing Lobbies You can list available lobbies using the `list` function. This function supports filtering using MongoDB-style filters: diff --git a/lib/peer.ts b/lib/peer.ts index 14da9a7..28792f4 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -289,6 +289,10 @@ export default class Peer { } } + get maxMessageSize (): number | null { + return this.conn.sctp?.maxMessageSize ?? null + } + toString (): string { return `[Peer: ${this.id}]` } From 413e76f6364f29ba1cb9b47226a5c8412f1f3bf0 Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Tue, 24 Mar 2026 03:18:13 +0100 Subject: [PATCH 2/2] Add fallback advice to documentation --- docs/api-reference.md | 1 + docs/basic-usage.md | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/api-reference.md b/docs/api-reference.md index a0980e0..eae50fe 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -117,3 +117,4 @@ Maximum message size, in bytes, that can be sent in a single WebRTC data channel Notes: - This value is negotiated per peer and can differ across connections. - Use this to decide when to chunk large payloads. +- If this is `null` or `0` you should use a conservative message size such as `16 * 1024` bytes for large payloads. diff --git a/docs/basic-usage.md b/docs/basic-usage.md index c71c525..bc0ebf1 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -104,14 +104,10 @@ network.on('disconnected', peer => { ```js network.on('connected', peer => { const limit = peer.maxMessageSize - if (limit === null) { - console.log(`No SCTP max message size available for ${peer.id} yet`) - return - } - if (limit === 0) { - console.log(`No practical message size limit reported for ${peer.id}`) - return + if (limit === null) { + limit = 16 * 1024 + console.log(`No SCTP max message size available for ${peer.id} yet, using conservative limit of ${limit} bytes`) } console.log(`Max payload for ${peer.id}: ${limit} bytes`)