Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
14 changes: 14 additions & 0 deletions docs/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 4 additions & 0 deletions lib/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export default class Peer {
}
}

get maxMessageSize (): number | null {
return this.conn.sctp?.maxMessageSize ?? null
}

toString (): string {
return `[Peer: ${this.id}]`
}
Expand Down
Loading