diff --git a/docs/api-reference.md b/docs/api-reference.md index e53081d..eae50fe 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,15 @@ 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. +- 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 5338ad9..bc0ebf1 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -100,6 +100,20 @@ network.on('disconnected', peer => { }) ``` +##### Inspecting Message Size Limits +```js +network.on('connected', peer => { + const limit = peer.maxMessageSize + + 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`) +}) +``` + #### 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}]` }