@ingfah-ai/client 0.1.0 → 0.2.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/CHANGELOG.md +15 -1
- package/README.md +113 -4
- package/dist/{index-9han0zre.js → index-j0qkkzx4.js} +141 -48
- package/dist/index.js +1 -1
- package/dist/peer.d.ts +6 -2
- package/dist/react.js +1 -1
- package/dist/room.d.ts +27 -3
- package/dist/signal.d.ts +20 -1
- package/dist/wire/messages.d.ts +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,21 @@ All notable changes to this package are documented in this file. The format foll
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the package adheres to
|
|
5
5
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
-
## [0.
|
|
7
|
+
## [0.2.0] - 2026-11-25
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- After a connection drop the client resumes its existing session instead of joining again, so
|
|
12
|
+
audio is not interrupted. It still joins again when the session cannot be resumed.
|
|
13
|
+
- The package is tested on Firefox and WebKit as well as Chromium.
|
|
14
|
+
|
|
15
|
+
## [0.1.1] - 2026-11-25
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- The README explains how to request an access token from the Ingfah API before joining a room.
|
|
20
|
+
|
|
21
|
+
## [0.1.0] - 2026-11-25
|
|
8
22
|
|
|
9
23
|
Initial release.
|
|
10
24
|
|
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ including voice agents.
|
|
|
7
7
|
- [Features](#features)
|
|
8
8
|
- [Requirements](#requirements)
|
|
9
9
|
- [Installation](#installation)
|
|
10
|
+
- [Getting an access token](#getting-an-access-token)
|
|
10
11
|
- [Usage](#usage)
|
|
11
12
|
- [React](#react)
|
|
12
13
|
- [API overview](#api-overview)
|
|
@@ -23,7 +24,8 @@ including voice agents.
|
|
|
23
24
|
a topic.
|
|
24
25
|
- Call methods on other participants and answer their calls.
|
|
25
26
|
- Read the state a voice agent reports, such as listening, thinking or speaking.
|
|
26
|
-
- Reconnect automatically after a network interruption,
|
|
27
|
+
- Reconnect automatically after a network interruption, resuming the same session where
|
|
28
|
+
possible and keeping the microphone published.
|
|
27
29
|
- Optional React bindings.
|
|
28
30
|
|
|
29
31
|
The package is distributed as ES modules with TypeScript declarations and has no runtime
|
|
@@ -31,10 +33,12 @@ dependencies.
|
|
|
31
33
|
|
|
32
34
|
## Requirements
|
|
33
35
|
|
|
34
|
-
- A browser with WebRTC support. The package is tested on Chromium
|
|
36
|
+
- A browser with WebRTC support. The package is tested on Chromium, Firefox and WebKit, the
|
|
37
|
+
engine of Safari.
|
|
35
38
|
- A secure context: `https://`, or `localhost` during development. Browsers do not allow
|
|
36
39
|
microphone access otherwise.
|
|
37
|
-
-
|
|
40
|
+
- An Ingfah API key, kept on your own server, to request an access token for each call. See
|
|
41
|
+
[Getting an access token](#getting-an-access-token).
|
|
38
42
|
- React 18 or later, only when the React bindings are used.
|
|
39
43
|
|
|
40
44
|
## Installation
|
|
@@ -47,6 +51,110 @@ npm install @ingfah-ai/client
|
|
|
47
51
|
bun add @ingfah-ai/client
|
|
48
52
|
```
|
|
49
53
|
|
|
54
|
+
## Getting an access token
|
|
55
|
+
|
|
56
|
+
A browser joins a room with a short-lived access token. Your server requests the token from the
|
|
57
|
+
Ingfah API with your API key and passes it to the browser. The API key must never be sent to the
|
|
58
|
+
browser.
|
|
59
|
+
|
|
60
|
+
```text
|
|
61
|
+
Browser Your server Ingfah API
|
|
62
|
+
| -- start a call --> | |
|
|
63
|
+
| | -- POST /get-call-token -->|
|
|
64
|
+
| | <-- access token ----------|
|
|
65
|
+
| <-- access token -- | |
|
|
66
|
+
| -- room.connect(serverUrl, token) ------------------------> room
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Request
|
|
70
|
+
|
|
71
|
+
```http
|
|
72
|
+
POST https://api.ingfah.ai/get-call-token
|
|
73
|
+
X-Api-Key: <your API key>
|
|
74
|
+
Content-Type: application/json
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"channel_identifier": "ingfah-voice-support",
|
|
80
|
+
"customer_context": {
|
|
81
|
+
"customer_identifier": "0812345678",
|
|
82
|
+
"plan": "gold"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
| Field | Required | Description |
|
|
88
|
+
| -------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
89
|
+
| `channel_identifier` | Yes | The channel of the AI team that answers the call. |
|
|
90
|
+
| `customer_context` | No | Text values passed to the agent as the customer's context. `customer_identifier` becomes the customer's identity in the room and defaults to `unknown`. |
|
|
91
|
+
| `sip_direction` | No | Passed to the agent unchanged. |
|
|
92
|
+
|
|
93
|
+
The API key needs the `session:get_call_token` scope.
|
|
94
|
+
|
|
95
|
+
### Response
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"status": "success",
|
|
100
|
+
"data": {
|
|
101
|
+
"token": "<access token>"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Each token is for a new room and is valid for 15 minutes. Request a new token for every call.
|
|
107
|
+
|
|
108
|
+
| Status | Meaning |
|
|
109
|
+
| ------ | -------------------------------------------------------- |
|
|
110
|
+
| `400` | The request body is not valid. |
|
|
111
|
+
| `401` | The API key is missing or not valid. |
|
|
112
|
+
| `403` | The license of the account has expired. |
|
|
113
|
+
| `404` | No AI team of the account has this `channel_identifier`. |
|
|
114
|
+
|
|
115
|
+
An error response has `"status": "error"`, a short `error` phrase and an `error_detail` message.
|
|
116
|
+
The wording of `error_detail` may change, so do not match on it.
|
|
117
|
+
|
|
118
|
+
### Example
|
|
119
|
+
|
|
120
|
+
On your server, expose an endpoint of your own that the browser can call:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// Runs on your server. Keep INGFAH_API_KEY out of the browser.
|
|
124
|
+
export async function createCallToken(customerId: string): Promise<string> {
|
|
125
|
+
const response = await fetch('https://api.ingfah.ai/get-call-token', {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {
|
|
128
|
+
'X-Api-Key': process.env.INGFAH_API_KEY!,
|
|
129
|
+
'Content-Type': 'application/json',
|
|
130
|
+
},
|
|
131
|
+
body: JSON.stringify({
|
|
132
|
+
channel_identifier: 'ingfah-voice-support',
|
|
133
|
+
customer_context: { customer_identifier: customerId },
|
|
134
|
+
}),
|
|
135
|
+
});
|
|
136
|
+
if (!response.ok) {
|
|
137
|
+
throw new Error(`Could not get a call token: ${response.status}`);
|
|
138
|
+
}
|
|
139
|
+
const body = (await response.json()) as { data: { token: string } };
|
|
140
|
+
return body.data.token;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
In the browser, fetch the token from your server and join the room:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { Room } from '@ingfah-ai/client';
|
|
148
|
+
|
|
149
|
+
const SERVER_URL = 'wss://core.ingfah.ai';
|
|
150
|
+
|
|
151
|
+
const { token } = await (await fetch('/api/call-token', { method: 'POST' })).json();
|
|
152
|
+
|
|
153
|
+
const room = new Room();
|
|
154
|
+
await room.connect(SERVER_URL, token);
|
|
155
|
+
await room.localParticipant.setMicrophoneEnabled(true);
|
|
156
|
+
```
|
|
157
|
+
|
|
50
158
|
## Usage
|
|
51
159
|
|
|
52
160
|
### Connect to a room
|
|
@@ -273,7 +381,8 @@ without its message.
|
|
|
273
381
|
## Limitations
|
|
274
382
|
|
|
275
383
|
- Text and byte streams are not supported.
|
|
276
|
-
-
|
|
384
|
+
- When the connection drops, the client first resumes its session, which keeps audio flowing. If
|
|
385
|
+
the server no longer has the session, it joins again, and remote audio stops briefly while it
|
|
277
386
|
does.
|
|
278
387
|
|
|
279
388
|
## License
|
|
@@ -466,9 +466,9 @@ class PeerLink {
|
|
|
466
466
|
return answer.sdp ?? "";
|
|
467
467
|
});
|
|
468
468
|
}
|
|
469
|
-
createOffer() {
|
|
469
|
+
createOffer(options = {}) {
|
|
470
470
|
return this.run(async () => {
|
|
471
|
-
const offer = await this.pc.createOffer();
|
|
471
|
+
const offer = await this.pc.createOffer(options);
|
|
472
472
|
await this.pc.setLocalDescription(offer);
|
|
473
473
|
return offer.sdp ?? "";
|
|
474
474
|
});
|
|
@@ -493,6 +493,9 @@ class PeerLink {
|
|
|
493
493
|
await this.pc.addIceCandidate(candidate);
|
|
494
494
|
}
|
|
495
495
|
}
|
|
496
|
+
get isNegotiated() {
|
|
497
|
+
return this.pc.remoteDescription !== null;
|
|
498
|
+
}
|
|
496
499
|
get isConnected() {
|
|
497
500
|
return this.pc.connectionState === "connected";
|
|
498
501
|
}
|
|
@@ -856,6 +859,8 @@ function decodeServerField(field) {
|
|
|
856
859
|
case 18:
|
|
857
860
|
case 20:
|
|
858
861
|
return { kind: "pong" };
|
|
862
|
+
case 19:
|
|
863
|
+
return { kind: "resumed" };
|
|
859
864
|
default:
|
|
860
865
|
return { kind: "other" };
|
|
861
866
|
}
|
|
@@ -1105,17 +1110,38 @@ class SignalChannel {
|
|
|
1105
1110
|
constructor(handlers) {
|
|
1106
1111
|
this.handlers = handlers;
|
|
1107
1112
|
}
|
|
1108
|
-
open(serverUrl, token, options) {
|
|
1109
|
-
const
|
|
1110
|
-
|
|
1113
|
+
async open(serverUrl, token, options) {
|
|
1114
|
+
const url = endpoint(serverUrl, token, "/rtc", options.autoSubscribe);
|
|
1115
|
+
const first = await this.connect(url, options.timeoutMs, "join", () => explainRefusal(serverUrl, token, options.autoSubscribe));
|
|
1116
|
+
if (first.kind !== "join") {
|
|
1117
|
+
throw new ConnectionError2("the server sent an unexpected first message", "closed");
|
|
1118
|
+
}
|
|
1119
|
+
this.startPinging({
|
|
1120
|
+
intervalSeconds: first.join.pingIntervalSeconds,
|
|
1121
|
+
timeoutSeconds: first.join.pingTimeoutSeconds
|
|
1122
|
+
});
|
|
1123
|
+
return first.join;
|
|
1124
|
+
}
|
|
1125
|
+
async resume(serverUrl, token, options) {
|
|
1126
|
+
const url = endpoint(serverUrl, token, "/rtc", options.autoSubscribe);
|
|
1127
|
+
url.searchParams.set("reconnect", "1");
|
|
1128
|
+
url.searchParams.set("sid", options.participantSid);
|
|
1129
|
+
url.searchParams.set("reconnect_reason", String(options.reason));
|
|
1130
|
+
await this.connect(url, options.timeoutMs, "resumed", async () => {
|
|
1131
|
+
return new ConnectionError2("the session could not be resumed", "closed");
|
|
1132
|
+
});
|
|
1133
|
+
this.startPinging(options.keepAlive);
|
|
1134
|
+
}
|
|
1135
|
+
connect(url, timeoutMs, expected, explain) {
|
|
1136
|
+
url.protocol = url.protocol.replace(/^http/, "ws");
|
|
1111
1137
|
return new Promise((resolve, reject) => {
|
|
1112
|
-
let
|
|
1113
|
-
const socket = new WebSocket(
|
|
1138
|
+
let established = false;
|
|
1139
|
+
const socket = new WebSocket(url);
|
|
1114
1140
|
socket.binaryType = "arraybuffer";
|
|
1115
1141
|
this.socket = socket;
|
|
1116
1142
|
const timer = setTimeout(() => {
|
|
1117
1143
|
fail(new ConnectionError2("the server did not answer in time", "timeout"));
|
|
1118
|
-
},
|
|
1144
|
+
}, timeoutMs);
|
|
1119
1145
|
const fail = (error) => {
|
|
1120
1146
|
clearTimeout(timer);
|
|
1121
1147
|
this.close();
|
|
@@ -1123,7 +1149,7 @@ class SignalChannel {
|
|
|
1123
1149
|
};
|
|
1124
1150
|
socket.onmessage = (event) => {
|
|
1125
1151
|
const message = decodeServerMessage(new Uint8Array(event.data));
|
|
1126
|
-
if (
|
|
1152
|
+
if (established) {
|
|
1127
1153
|
if (message.kind === "pong") {
|
|
1128
1154
|
this.lastPong = Date.now();
|
|
1129
1155
|
} else {
|
|
@@ -1131,11 +1157,10 @@ class SignalChannel {
|
|
|
1131
1157
|
}
|
|
1132
1158
|
return;
|
|
1133
1159
|
}
|
|
1134
|
-
if (message.kind ===
|
|
1135
|
-
|
|
1160
|
+
if (message.kind === expected) {
|
|
1161
|
+
established = true;
|
|
1136
1162
|
clearTimeout(timer);
|
|
1137
|
-
|
|
1138
|
-
resolve(message.join);
|
|
1163
|
+
resolve(message);
|
|
1139
1164
|
} else if (message.kind === "leave") {
|
|
1140
1165
|
fail(new ConnectionError2("the server refused the connection", "refused"));
|
|
1141
1166
|
}
|
|
@@ -1144,12 +1169,12 @@ class SignalChannel {
|
|
|
1144
1169
|
if (this.closed) {
|
|
1145
1170
|
return;
|
|
1146
1171
|
}
|
|
1147
|
-
if (
|
|
1172
|
+
if (established) {
|
|
1148
1173
|
this.close();
|
|
1149
1174
|
this.handlers.onLost("the control channel closed");
|
|
1150
1175
|
return;
|
|
1151
1176
|
}
|
|
1152
|
-
|
|
1177
|
+
explain().then(fail);
|
|
1153
1178
|
};
|
|
1154
1179
|
});
|
|
1155
1180
|
}
|
|
@@ -1175,20 +1200,20 @@ class SignalChannel {
|
|
|
1175
1200
|
this.handlers.onLost("simulated loss");
|
|
1176
1201
|
}
|
|
1177
1202
|
}
|
|
1178
|
-
startPinging(
|
|
1179
|
-
if (
|
|
1203
|
+
startPinging(keepAlive) {
|
|
1204
|
+
if (keepAlive.intervalSeconds <= 0) {
|
|
1180
1205
|
return;
|
|
1181
1206
|
}
|
|
1182
|
-
const allowedSilence = (
|
|
1207
|
+
const allowedSilence = (keepAlive.intervalSeconds + keepAlive.timeoutSeconds) * 1000;
|
|
1183
1208
|
this.lastPong = Date.now();
|
|
1184
1209
|
this.pingTimer = setInterval(() => {
|
|
1185
|
-
if (
|
|
1210
|
+
if (keepAlive.timeoutSeconds > 0 && Date.now() - this.lastPong > allowedSilence) {
|
|
1186
1211
|
this.close();
|
|
1187
1212
|
this.handlers.onLost("the server stopped answering");
|
|
1188
1213
|
return;
|
|
1189
1214
|
}
|
|
1190
1215
|
this.send({ kind: "ping", timestamp: Date.now() });
|
|
1191
|
-
},
|
|
1216
|
+
}, keepAlive.intervalSeconds * 1000);
|
|
1192
1217
|
}
|
|
1193
1218
|
}
|
|
1194
1219
|
async function explainRefusal(serverUrl, token, autoSubscribe) {
|
|
@@ -1260,7 +1285,11 @@ var RoomEvent2;
|
|
|
1260
1285
|
})(RoomEvent2 ||= {});
|
|
1261
1286
|
const PUBLISH_TIMEOUT_MS = 1e4;
|
|
1262
1287
|
const CHANNEL_TIMEOUT_MS = 1e4;
|
|
1288
|
+
const RESUME_TIMEOUT_MS = 7000;
|
|
1263
1289
|
const RETRY_DELAYS_MS = [0, 300, 1000, 2000, 4000, 8000];
|
|
1290
|
+
const LOST_SIGNAL = 1;
|
|
1291
|
+
const LOST_SENDER = 2;
|
|
1292
|
+
const LOST_RECEIVER = 3;
|
|
1264
1293
|
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1265
1294
|
function withTimeout(promise, ms, error) {
|
|
1266
1295
|
return new Promise((resolve, reject) => {
|
|
@@ -1296,6 +1325,9 @@ class Room2 extends Emitter {
|
|
|
1296
1325
|
offerId = 0;
|
|
1297
1326
|
negotiation;
|
|
1298
1327
|
negotiateAgain = false;
|
|
1328
|
+
keepAlive = { intervalSeconds: 0, timeoutSeconds: 0 };
|
|
1329
|
+
expectCompleteList = false;
|
|
1330
|
+
skipResume = false;
|
|
1299
1331
|
answerWaiter;
|
|
1300
1332
|
publishWaiters = new Map;
|
|
1301
1333
|
unclaimed = [];
|
|
@@ -1352,7 +1384,8 @@ class Room2 extends Emitter {
|
|
|
1352
1384
|
unregisterRpcMethod(method) {
|
|
1353
1385
|
this.calls.unregister(method);
|
|
1354
1386
|
}
|
|
1355
|
-
simulateConnectionLoss() {
|
|
1387
|
+
simulateConnectionLoss(recovery = "resume") {
|
|
1388
|
+
this.skipResume = recovery === "rejoin";
|
|
1356
1389
|
this.signal?.simulateLoss();
|
|
1357
1390
|
}
|
|
1358
1391
|
handlePageHide = () => {
|
|
@@ -1378,18 +1411,7 @@ class Room2 extends Emitter {
|
|
|
1378
1411
|
const generation = this.generation;
|
|
1379
1412
|
const current = () => generation === this.generation;
|
|
1380
1413
|
const timeoutMs = session.options.connectTimeout ?? 15000;
|
|
1381
|
-
const signal =
|
|
1382
|
-
onMessage: (message) => {
|
|
1383
|
-
if (current()) {
|
|
1384
|
-
this.handleServerMessage(message);
|
|
1385
|
-
}
|
|
1386
|
-
},
|
|
1387
|
-
onLost: (why) => {
|
|
1388
|
-
if (current()) {
|
|
1389
|
-
this.handleConnectionLost(why);
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
});
|
|
1414
|
+
const signal = this.createSignal(generation);
|
|
1393
1415
|
this.signal = signal;
|
|
1394
1416
|
const join = await signal.open(session.url, session.token, {
|
|
1395
1417
|
autoSubscribe: session.options.autoSubscribe !== false,
|
|
@@ -1398,12 +1420,16 @@ class Room2 extends Emitter {
|
|
|
1398
1420
|
if (!current()) {
|
|
1399
1421
|
throw new ConnectionError2("the connection attempt was cancelled", "cancelled");
|
|
1400
1422
|
}
|
|
1423
|
+
this.keepAlive = {
|
|
1424
|
+
intervalSeconds: join.pingIntervalSeconds,
|
|
1425
|
+
timeoutSeconds: join.pingTimeoutSeconds
|
|
1426
|
+
};
|
|
1401
1427
|
this.sid = join.room.sid;
|
|
1402
1428
|
this.name = join.room.name;
|
|
1403
1429
|
this.metadata = join.room.metadata;
|
|
1404
1430
|
this.localParticipant.applyProfile(join.participant);
|
|
1405
1431
|
this.openPeerLinks(join, session.options.rtcConfig);
|
|
1406
|
-
this.syncParticipants(join.others,
|
|
1432
|
+
this.syncParticipants(join.others, "initial");
|
|
1407
1433
|
const first = join.receiveFirst ? this.receiver : this.sender;
|
|
1408
1434
|
if (!join.receiveFirst || join.publishEarly) {
|
|
1409
1435
|
this.negotiate().catch(() => {});
|
|
@@ -1417,6 +1443,21 @@ class Room2 extends Emitter {
|
|
|
1417
1443
|
this.abortConnect = undefined;
|
|
1418
1444
|
}
|
|
1419
1445
|
}
|
|
1446
|
+
createSignal(generation) {
|
|
1447
|
+
const current = () => generation === this.generation;
|
|
1448
|
+
return new SignalChannel({
|
|
1449
|
+
onMessage: (message) => {
|
|
1450
|
+
if (current()) {
|
|
1451
|
+
this.handleServerMessage(message);
|
|
1452
|
+
}
|
|
1453
|
+
},
|
|
1454
|
+
onLost: (why) => {
|
|
1455
|
+
if (current()) {
|
|
1456
|
+
this.handleConnectionLost(why, LOST_SIGNAL);
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1420
1461
|
openPeerLinks(join, overrides) {
|
|
1421
1462
|
const config = {
|
|
1422
1463
|
iceServers: join.iceServers.map((server) => ({
|
|
@@ -1427,7 +1468,6 @@ class Room2 extends Emitter {
|
|
|
1427
1468
|
...join.forceRelay ? { iceTransportPolicy: "relay" } : {},
|
|
1428
1469
|
...overrides
|
|
1429
1470
|
};
|
|
1430
|
-
const signal = this.signal;
|
|
1431
1471
|
const receiver = new PeerLink(config);
|
|
1432
1472
|
const sender = new PeerLink(config);
|
|
1433
1473
|
this.receiver = receiver;
|
|
@@ -1437,9 +1477,12 @@ class Room2 extends Emitter {
|
|
|
1437
1477
|
[receiver, 1]
|
|
1438
1478
|
]) {
|
|
1439
1479
|
link.onCandidate = (candidate) => {
|
|
1440
|
-
signal
|
|
1480
|
+
this.signal?.send({
|
|
1481
|
+
kind: "trickle",
|
|
1482
|
+
trickle: { candidate: JSON.stringify(candidate), target }
|
|
1483
|
+
});
|
|
1441
1484
|
};
|
|
1442
|
-
link.onFailed = () => this.handleConnectionLost("the media connection failed");
|
|
1485
|
+
link.onFailed = () => this.handleConnectionLost("the media connection failed", target === 0 ? LOST_SENDER : LOST_RECEIVER);
|
|
1443
1486
|
}
|
|
1444
1487
|
receiver.pc.ontrack = (event) => this.handleIncomingMedia(event);
|
|
1445
1488
|
receiver.pc.ondatachannel = (event) => this.listenTo(event.channel);
|
|
@@ -1494,20 +1537,67 @@ class Room2 extends Emitter {
|
|
|
1494
1537
|
this.setState("disconnected" /* Disconnected */);
|
|
1495
1538
|
this.emit("disconnected" /* Disconnected */, reason);
|
|
1496
1539
|
}
|
|
1497
|
-
handleConnectionLost(why) {
|
|
1540
|
+
handleConnectionLost(why, reason, canResume = true) {
|
|
1498
1541
|
if (this.state === "connecting" /* Connecting */) {
|
|
1499
1542
|
this.abortConnect?.(new ConnectionError2(why, "closed"));
|
|
1500
1543
|
} else if (this.state === "connected" /* Connected */) {
|
|
1501
|
-
this.
|
|
1544
|
+
const resume = canResume && !this.skipResume;
|
|
1545
|
+
this.skipResume = false;
|
|
1546
|
+
this.recover(reason, resume);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
async resume(session, reason) {
|
|
1550
|
+
const receiver = this.receiver;
|
|
1551
|
+
const sender = this.sender;
|
|
1552
|
+
if (!receiver || !sender || !this.localParticipant.sid) {
|
|
1553
|
+
return false;
|
|
1554
|
+
}
|
|
1555
|
+
this.generation += 1;
|
|
1556
|
+
const generation = this.generation;
|
|
1557
|
+
this.signal?.close();
|
|
1558
|
+
const signal = this.createSignal(generation);
|
|
1559
|
+
this.signal = signal;
|
|
1560
|
+
this.answerWaiter?.reject(new ConnectionError2("the connection was interrupted", "closed"));
|
|
1561
|
+
this.answerWaiter = undefined;
|
|
1562
|
+
this.negotiation = undefined;
|
|
1563
|
+
this.negotiateAgain = false;
|
|
1564
|
+
try {
|
|
1565
|
+
this.expectCompleteList = true;
|
|
1566
|
+
await signal.resume(session.url, session.token, {
|
|
1567
|
+
autoSubscribe: session.options.autoSubscribe !== false,
|
|
1568
|
+
timeoutMs: RESUME_TIMEOUT_MS,
|
|
1569
|
+
participantSid: this.localParticipant.sid,
|
|
1570
|
+
reason,
|
|
1571
|
+
keepAlive: this.keepAlive
|
|
1572
|
+
});
|
|
1573
|
+
if (sender.isNegotiated) {
|
|
1574
|
+
await this.negotiate({ iceRestart: true });
|
|
1575
|
+
await sender.waitUntilConnected(RESUME_TIMEOUT_MS);
|
|
1576
|
+
}
|
|
1577
|
+
await receiver.waitUntilConnected(RESUME_TIMEOUT_MS);
|
|
1578
|
+
return generation === this.generation;
|
|
1579
|
+
} catch {
|
|
1580
|
+
return false;
|
|
1502
1581
|
}
|
|
1503
1582
|
}
|
|
1504
|
-
async recover() {
|
|
1583
|
+
async recover(reason, canResume) {
|
|
1505
1584
|
const session = this.session;
|
|
1506
1585
|
if (!session || this.state !== "connected" /* Connected */) {
|
|
1507
1586
|
return;
|
|
1508
1587
|
}
|
|
1509
1588
|
this.setState("reconnecting" /* Reconnecting */);
|
|
1510
1589
|
this.emit("reconnecting" /* Reconnecting */);
|
|
1590
|
+
if (canResume && await this.resume(session, reason)) {
|
|
1591
|
+
if (this.isIn("reconnecting" /* Reconnecting */)) {
|
|
1592
|
+
this.setState("connected" /* Connected */);
|
|
1593
|
+
this.emit("reconnected" /* Reconnected */);
|
|
1594
|
+
}
|
|
1595
|
+
return;
|
|
1596
|
+
}
|
|
1597
|
+
if (!this.isIn("reconnecting" /* Reconnecting */)) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
this.expectCompleteList = false;
|
|
1511
1601
|
this.generation += 1;
|
|
1512
1602
|
this.closeTransport();
|
|
1513
1603
|
for (const participant of this.remoteParticipants.values()) {
|
|
@@ -1556,7 +1646,7 @@ class Room2 extends Emitter {
|
|
|
1556
1646
|
kind: "answer",
|
|
1557
1647
|
description: { type: "answer", sdp, id: message.description.id }
|
|
1558
1648
|
});
|
|
1559
|
-
}).catch(() => this.handleConnectionLost("the server offer could not be applied"));
|
|
1649
|
+
}).catch(() => this.handleConnectionLost("the server offer could not be applied", LOST_RECEIVER));
|
|
1560
1650
|
break;
|
|
1561
1651
|
case "answer":
|
|
1562
1652
|
this.sender?.acceptAnswer(message.description.sdp).then(() => this.answerWaiter?.resolve(), (error) => this.answerWaiter?.reject(error));
|
|
@@ -1570,7 +1660,8 @@ class Room2 extends Emitter {
|
|
|
1570
1660
|
break;
|
|
1571
1661
|
}
|
|
1572
1662
|
case "participants":
|
|
1573
|
-
this.syncParticipants(message.participants,
|
|
1663
|
+
this.syncParticipants(message.participants, this.expectCompleteList ? "complete" : "update");
|
|
1664
|
+
this.expectCompleteList = false;
|
|
1574
1665
|
break;
|
|
1575
1666
|
case "trackPublished":
|
|
1576
1667
|
this.publishWaiters.get(message.cid)?.resolve(message.track);
|
|
@@ -1598,24 +1689,24 @@ class Room2 extends Emitter {
|
|
|
1598
1689
|
if (message.leave.action === 0) {
|
|
1599
1690
|
this.finish(message.leave.reason);
|
|
1600
1691
|
} else {
|
|
1601
|
-
this.handleConnectionLost("the server asked us to reconnect");
|
|
1692
|
+
this.handleConnectionLost("the server asked us to reconnect", LOST_SIGNAL, message.leave.action === 1);
|
|
1602
1693
|
}
|
|
1603
1694
|
break;
|
|
1604
1695
|
default:
|
|
1605
1696
|
break;
|
|
1606
1697
|
}
|
|
1607
1698
|
}
|
|
1608
|
-
syncParticipants(infos,
|
|
1699
|
+
syncParticipants(infos, mode) {
|
|
1609
1700
|
for (const info of infos) {
|
|
1610
1701
|
if (info.sid === this.localParticipant.sid || info.identity === this.localParticipant.identity) {
|
|
1611
1702
|
this.applyLocalInfo(info);
|
|
1612
1703
|
} else if (info.state === 3) {
|
|
1613
1704
|
this.removeParticipant(info.identity);
|
|
1614
1705
|
} else {
|
|
1615
|
-
this.applyRemoteInfo(info, initial);
|
|
1706
|
+
this.applyRemoteInfo(info, mode === "initial");
|
|
1616
1707
|
}
|
|
1617
1708
|
}
|
|
1618
|
-
if (
|
|
1709
|
+
if (mode !== "update") {
|
|
1619
1710
|
const present = new Set(infos.map((info) => info.identity));
|
|
1620
1711
|
for (const identity of [...this.remoteParticipants.keys()]) {
|
|
1621
1712
|
if (!present.has(identity)) {
|
|
@@ -1751,11 +1842,12 @@ class Room2 extends Emitter {
|
|
|
1751
1842
|
this.activeSpeakers = everyone.filter((participant) => participant.isSpeaking).sort((a, b) => b.audioLevel - a.audioLevel);
|
|
1752
1843
|
this.emit("activeSpeakersChanged" /* ActiveSpeakersChanged */, this.activeSpeakers);
|
|
1753
1844
|
}
|
|
1754
|
-
negotiate() {
|
|
1845
|
+
negotiate(options = {}) {
|
|
1755
1846
|
if (this.negotiation) {
|
|
1756
1847
|
this.negotiateAgain = true;
|
|
1757
1848
|
return this.negotiation;
|
|
1758
1849
|
}
|
|
1850
|
+
let first = true;
|
|
1759
1851
|
const round = async () => {
|
|
1760
1852
|
do {
|
|
1761
1853
|
this.negotiateAgain = false;
|
|
@@ -1764,7 +1856,8 @@ class Room2 extends Emitter {
|
|
|
1764
1856
|
if (!sender || !signal) {
|
|
1765
1857
|
throw new ConnectionError2("the room is not connected", "closed");
|
|
1766
1858
|
}
|
|
1767
|
-
const sdp = await sender.createOffer();
|
|
1859
|
+
const sdp = await sender.createOffer(first ? options : {});
|
|
1860
|
+
first = false;
|
|
1768
1861
|
const answered = new Promise((resolve, reject) => {
|
|
1769
1862
|
this.answerWaiter = { resolve, reject };
|
|
1770
1863
|
});
|
package/dist/index.js
CHANGED
package/dist/peer.d.ts
CHANGED
|
@@ -12,12 +12,16 @@ export declare class PeerLink {
|
|
|
12
12
|
private run;
|
|
13
13
|
/** Accepts an offer from the server and returns our answer. */
|
|
14
14
|
answerOffer(sdp: string): Promise<string>;
|
|
15
|
-
/** Creates an offer for the server. */
|
|
16
|
-
createOffer(
|
|
15
|
+
/** Creates an offer for the server. With `iceRestart`, it asks for fresh network paths. */
|
|
16
|
+
createOffer(options?: {
|
|
17
|
+
iceRestart?: boolean;
|
|
18
|
+
}): Promise<string>;
|
|
17
19
|
/** Accepts the server's answer to our offer. */
|
|
18
20
|
acceptAnswer(sdp: string): Promise<void>;
|
|
19
21
|
addCandidate(candidate: RTCIceCandidateInit): Promise<void>;
|
|
20
22
|
private flushEarlyCandidates;
|
|
23
|
+
/** True once this connection has exchanged descriptions with the server. */
|
|
24
|
+
get isNegotiated(): boolean;
|
|
21
25
|
get isConnected(): boolean;
|
|
22
26
|
/** Resolves once media can flow. Rejects when that takes too long or the link fails. */
|
|
23
27
|
waitUntilConnected(timeoutMs: number): Promise<void>;
|
package/dist/react.js
CHANGED
package/dist/room.d.ts
CHANGED
|
@@ -164,6 +164,11 @@ export declare class Room extends Emitter<RoomEvents> {
|
|
|
164
164
|
private offerId;
|
|
165
165
|
private negotiation;
|
|
166
166
|
private negotiateAgain;
|
|
167
|
+
private keepAlive;
|
|
168
|
+
/** Set after a resume: the next participant list from the server is complete. */
|
|
169
|
+
private expectCompleteList;
|
|
170
|
+
/** Set by tests to skip resuming and go straight to a fresh join. */
|
|
171
|
+
private skipResume;
|
|
167
172
|
private answerWaiter;
|
|
168
173
|
private readonly publishWaiters;
|
|
169
174
|
/** Media that arrived before the server told us whose it is. */
|
|
@@ -183,8 +188,11 @@ export declare class Room extends Emitter<RoomEvents> {
|
|
|
183
188
|
/** Answers calls to `method` from other participants. The handler returns the answer text. */
|
|
184
189
|
registerRpcMethod(method: string, handler: RpcHandler): void;
|
|
185
190
|
unregisterRpcMethod(method: string): void;
|
|
186
|
-
/**
|
|
187
|
-
|
|
191
|
+
/**
|
|
192
|
+
* Drops the connection as if the network had failed, to exercise reconnection in tests.
|
|
193
|
+
* `resume` lets the room pick up the same session; `rejoin` forces a fresh join.
|
|
194
|
+
*/
|
|
195
|
+
simulateConnectionLoss(recovery?: 'resume' | 'rejoin'): void;
|
|
188
196
|
private readonly handlePageHide;
|
|
189
197
|
/** Reads the state without letting the compiler assume it cannot change across an await. */
|
|
190
198
|
private isIn;
|
|
@@ -192,14 +200,30 @@ export declare class Room extends Emitter<RoomEvents> {
|
|
|
192
200
|
private setCanPlaybackAudio;
|
|
193
201
|
/** Opens the control channel and both peer connections. */
|
|
194
202
|
private establish;
|
|
203
|
+
/** A control channel whose callbacks are ignored once the connection has been replaced. */
|
|
204
|
+
private createSignal;
|
|
195
205
|
private openPeerLinks;
|
|
196
206
|
private closeTransport;
|
|
197
207
|
/** Ends the room for good and tells the app why. */
|
|
198
208
|
private finish;
|
|
199
209
|
private handleConnectionLost;
|
|
200
|
-
/**
|
|
210
|
+
/**
|
|
211
|
+
* Picks the session up again on a new control channel. The media connections are kept, so
|
|
212
|
+
* audio keeps flowing where the network allows it. Returns false when the server no longer
|
|
213
|
+
* has the session or the media does not come back.
|
|
214
|
+
*/
|
|
215
|
+
private resume;
|
|
216
|
+
/**
|
|
217
|
+
* Gets the room back after the connection dropped: first by resuming the session, and if that
|
|
218
|
+
* does not work, by joining again and publishing the microphone again.
|
|
219
|
+
*/
|
|
201
220
|
private recover;
|
|
202
221
|
private handleServerMessage;
|
|
222
|
+
/**
|
|
223
|
+
* Applies what the server said about participants. `initial` is the list at join, whose
|
|
224
|
+
* members are not announced. `complete` is a full list after a resume: anyone missing has
|
|
225
|
+
* left. `update` lists only those who changed.
|
|
226
|
+
*/
|
|
203
227
|
private syncParticipants;
|
|
204
228
|
private applyLocalInfo;
|
|
205
229
|
private announceProfileChanges;
|
package/dist/signal.d.ts
CHANGED
|
@@ -8,6 +8,18 @@ export interface OpenOptions {
|
|
|
8
8
|
autoSubscribe: boolean;
|
|
9
9
|
timeoutMs: number;
|
|
10
10
|
}
|
|
11
|
+
/** How often to ping, and how long the server may stay silent. Zero means no keep-alive. */
|
|
12
|
+
export interface KeepAlive {
|
|
13
|
+
intervalSeconds: number;
|
|
14
|
+
timeoutSeconds: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ResumeOptions extends OpenOptions {
|
|
17
|
+
/** The id the server gave this participant when it joined. */
|
|
18
|
+
participantSid: string;
|
|
19
|
+
/** Why we reconnect: 1 the control channel dropped, 2 or 3 a media connection failed. */
|
|
20
|
+
reason: number;
|
|
21
|
+
keepAlive: KeepAlive;
|
|
22
|
+
}
|
|
11
23
|
/** The control channel to the server: one WebSocket carrying encoded messages. */
|
|
12
24
|
export declare class SignalChannel {
|
|
13
25
|
private readonly handlers;
|
|
@@ -16,8 +28,15 @@ export declare class SignalChannel {
|
|
|
16
28
|
private lastPong;
|
|
17
29
|
private closed;
|
|
18
30
|
constructor(handlers: SignalHandlers);
|
|
19
|
-
/**
|
|
31
|
+
/** Joins as a new participant. Resolves with the server's welcome. */
|
|
20
32
|
open(serverUrl: string, token: string, options: OpenOptions): Promise<JoinInfo>;
|
|
33
|
+
/**
|
|
34
|
+
* Reconnects to the session this participant already has, keeping its media connections.
|
|
35
|
+
* Rejects when the server no longer has that session.
|
|
36
|
+
*/
|
|
37
|
+
resume(serverUrl: string, token: string, options: ResumeOptions): Promise<void>;
|
|
38
|
+
/** Opens the socket and resolves with the first message, which must be of kind `expected`. */
|
|
39
|
+
private connect;
|
|
21
40
|
send(message: ClientMessage): void;
|
|
22
41
|
close(): void;
|
|
23
42
|
/** Drops the channel as if the network had failed. For tests. */
|
package/dist/wire/messages.d.ts
CHANGED