diff --git a/packages/client/CHANGELOG.md b/packages/client/CHANGELOG.md index cc8ded470..8d1a9c5c1 100644 --- a/packages/client/CHANGELOG.md +++ b/packages/client/CHANGELOG.md @@ -2,6 +2,13 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [1.11.12](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-client-1.11.11...@stream-io/video-client-1.11.12) (2024-12-03) + + +### Bug Fixes + +* handle timeout on SFU WS connections ([#1600](https://github.com/GetStream/stream-video-js/issues/1600)) ([5f2db7b](https://github.com/GetStream/stream-video-js/commit/5f2db7bd5cfdf57cdc04d6a6ed752f43e5b06657)) + ## [1.11.11](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-client-1.11.10...@stream-io/video-client-1.11.11) (2024-11-29) diff --git a/packages/client/package.json b/packages/client/package.json index 55d64defc..410ae0d6b 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,6 +1,6 @@ { "name": "@stream-io/video-client", - "version": "1.11.11", + "version": "1.11.12", "packageManager": "yarn@3.2.4", "main": "dist/index.cjs.js", "module": "dist/index.es.js", diff --git a/packages/client/src/Call.ts b/packages/client/src/Call.ts index 519553cd2..58469624d 100644 --- a/packages/client/src/Call.ts +++ b/packages/client/src/Call.ts @@ -124,9 +124,10 @@ import { getSdkSignature } from './stats/utils'; import { withoutConcurrency } from './helpers/concurrency'; import { ensureExhausted } from './helpers/ensureExhausted'; import { + makeSafePromise, PromiseWithResolvers, promiseWithResolvers, -} from './helpers/withResolvers'; +} from './helpers/promise'; /** * An object representation of a `Call`. @@ -218,6 +219,7 @@ export class Call { private reconnectAttempts = 0; private reconnectStrategy = WebsocketReconnectStrategy.UNSPECIFIED; private fastReconnectDeadlineSeconds: number = 0; + private disconnectionTimeoutSeconds: number = 0; private lastOfflineTimestamp: number = 0; private networkAvailableTask: PromiseWithResolvers | undefined; // maintain the order of publishing tracks to restore them after a reconnection @@ -1110,6 +1112,9 @@ export class Call { */ private handleSfuSignalClose = (sfuClient: StreamSfuClient) => { this.logger('debug', '[Reconnect] SFU signal connection closed'); + // SFU WS closed before we finished current join, no need to schedule reconnect + // because join operation will fail + if (this.state.callingState === CallingState.JOINING) return; // normal close, no need to reconnect if (sfuClient.isLeaving) return; this.reconnect(WebsocketReconnectStrategy.REJOIN).catch((err) => { @@ -1127,14 +1132,35 @@ export class Call { private reconnect = async ( strategy: WebsocketReconnectStrategy, ): Promise => { + if ( + this.state.callingState === CallingState.RECONNECTING || + this.state.callingState === CallingState.RECONNECTING_FAILED + ) + return; + return withoutConcurrency(this.reconnectConcurrencyTag, async () => { this.logger( 'info', `[Reconnect] Reconnecting with strategy ${WebsocketReconnectStrategy[strategy]}`, ); + let reconnectStartTime = Date.now(); this.reconnectStrategy = strategy; + do { + if ( + this.disconnectionTimeoutSeconds > 0 && + (Date.now() - reconnectStartTime) / 1000 > + this.disconnectionTimeoutSeconds + ) { + this.logger( + 'warn', + '[Reconnect] Stopping reconnection attempts after reaching disconnection timeout', + ); + this.state.setCallingState(CallingState.RECONNECTING_FAILED); + return; + } + // we don't increment reconnect attempts for the FAST strategy. if (this.reconnectStrategy !== WebsocketReconnectStrategy.FAST) { this.reconnectAttempts++; @@ -1252,7 +1278,7 @@ export class Call { currentSubscriber?.detachEventHandlers(); currentPublisher?.detachEventHandlers(); - const migrationTask = currentSfuClient.enterMigration(); + const migrationTask = makeSafePromise(currentSfuClient.enterMigration()); try { const currentSfu = currentSfuClient.edgeName; @@ -1270,7 +1296,7 @@ export class Call { // Wait for the migration to complete, then close the previous SFU client // and the peer connection instances. In case of failure, the migration // task would throw an error and REJOIN would be attempted. - await migrationTask; + await migrationTask(); // in MIGRATE, we can consider the call as joined only after // `participantMigrationComplete` event is received, signaled by @@ -2396,4 +2422,13 @@ export class Call { ); this.dynascaleManager.applyTrackSubscriptions(); }; + + /** + * Sets the maximum amount of time a user can remain waiting for a reconnect + * after a network disruption + * @param timeoutSeconds Timeout in seconds, or 0 to keep reconnecting indefinetely + */ + setDisconnectionTimeout = (timeoutSeconds: number) => { + this.disconnectionTimeoutSeconds = timeoutSeconds; + }; } diff --git a/packages/client/src/StreamSfuClient.ts b/packages/client/src/StreamSfuClient.ts index eca72c36c..40f80898e 100644 --- a/packages/client/src/StreamSfuClient.ts +++ b/packages/client/src/StreamSfuClient.ts @@ -25,15 +25,16 @@ import { } from './gen/video/sfu/signal_rpc/signal'; import { ICETrickle, TrackType } from './gen/video/sfu/models/models'; import { StreamClient } from './coordinator/connection/client'; -import { generateUUIDv4, sleep } from './coordinator/connection/utils'; +import { generateUUIDv4 } from './coordinator/connection/utils'; import { Credentials } from './gen/coordinator'; import { Logger } from './coordinator/connection/types'; import { getLogger, getLogLevel } from './logger'; -import { withoutConcurrency } from './helpers/concurrency'; import { promiseWithResolvers, PromiseWithResolvers, -} from './helpers/withResolvers'; + makeSafePromise, + SafePromise, +} from './helpers/promise'; export type StreamSfuClientConstructor = { /** @@ -101,7 +102,7 @@ export class StreamSfuClient { /** * Promise that resolves when the WebSocket connection is ready (open). */ - private signalReady!: Promise; + private signalReady!: SafePromise; /** * Flag to indicate if the client is in the process of leaving the call. @@ -116,7 +117,6 @@ export class StreamSfuClient { private pingIntervalInMs = 10 * 1000; private unhealthyTimeoutInMs = this.pingIntervalInMs + 5 * 1000; private lastMessageTimestamp?: Date; - private readonly restoreWebSocketConcurrencyTag = Symbol('recoverWebSocket'); private readonly unsubscribeIceTrickle: () => void; private readonly unsubscribeNetworkChanged: () => void; private readonly onSignalClose: (() => void) | undefined; @@ -124,7 +124,7 @@ export class StreamSfuClient { private readonly logTag: string; private readonly credentials: Credentials; private readonly dispatcher: Dispatcher; - private readonly joinResponseTimeout?: number; + private readonly joinResponseTimeout: number; private networkAvailableTask: PromiseWithResolvers | undefined; /** * Promise that resolves when the JoinResponse is received. @@ -227,32 +227,31 @@ export class StreamSfuClient { }); this.signalWs.addEventListener('close', this.handleWebSocketClose); - this.signalWs.addEventListener('error', this.restoreWebSocket); - - this.signalReady = new Promise((resolve) => { - const onOpen = () => { - this.signalWs.removeEventListener('open', onOpen); - resolve(this.signalWs); - }; - this.signalWs.addEventListener('open', onOpen); - }); + + this.signalReady = makeSafePromise( + Promise.race([ + new Promise((resolve) => { + const onOpen = () => { + this.signalWs.removeEventListener('open', onOpen); + resolve(this.signalWs); + }; + this.signalWs.addEventListener('open', onOpen); + }), + + new Promise((resolve, reject) => { + setTimeout( + () => reject(new Error('SFU WS connection timed out')), + this.joinResponseTimeout, + ); + }), + ]), + ); }; private cleanUpWebSocket = () => { - this.signalWs.removeEventListener('error', this.restoreWebSocket); this.signalWs.removeEventListener('close', this.handleWebSocketClose); }; - private restoreWebSocket = () => { - withoutConcurrency(this.restoreWebSocketConcurrencyTag, async () => { - await this.networkAvailableTask?.promise; - this.logger('debug', 'Restoring SFU WS connection'); - this.cleanUpWebSocket(); - await sleep(500); - this.createWebSocket(); - }).catch((err) => this.logger('debug', `Can't restore WS connection`, err)); - }; - get isHealthy() { return this.signalWs.readyState === WebSocket.OPEN; } @@ -409,7 +408,7 @@ export class StreamSfuClient { data: Omit, ): Promise => { // wait for the signal web socket to be ready before sending "joinRequest" - await this.signalReady; + await this.signalReady(); if (this.joinResponseTask.isResolved || this.joinResponseTask.isRejected) { // we need to lock the RPC requests until we receive a JoinResponse. // that's why we have this primitive lock mechanism. @@ -478,7 +477,7 @@ export class StreamSfuClient { }; private send = async (message: SfuRequest) => { - await this.signalReady; // wait for the signal ws to be open + await this.signalReady(); // wait for the signal ws to be open const msgJson = SfuRequest.toJson(message); if (this.signalWs.readyState !== WebSocket.OPEN) { this.logger('debug', 'Signal WS is not open. Skipping message', msgJson); diff --git a/packages/client/src/helpers/promise.ts b/packages/client/src/helpers/promise.ts index 482bc7c07..f607ce505 100644 --- a/packages/client/src/helpers/promise.ts +++ b/packages/client/src/helpers/promise.ts @@ -45,3 +45,47 @@ export function makeSafePromise(promise: Promise): SafePromise { unwrapPromise.checkPending = () => isPending; return unwrapPromise; } + +export type PromiseWithResolvers = { + promise: Promise; + resolve: (value: T | PromiseLike) => void; + reject: (reason: any) => void; + isResolved: boolean; + isRejected: boolean; +}; + +/** + * Creates a new promise with resolvers. + * + * Based on: + * - https://github.com/tc39/proposal-promise-with-resolvers/blob/main/polyfills.js + */ +export const promiseWithResolvers = (): PromiseWithResolvers => { + let resolve: (value: T | PromiseLike) => void; + let reject: (reason: any) => void; + const promise = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + + let isResolved = false; + let isRejected = false; + + const resolver = (value: T | PromiseLike) => { + isResolved = true; + resolve(value); + }; + + const rejecter = (reason: any) => { + isRejected = true; + reject(reason); + }; + + return { + promise, + resolve: resolver, + reject: rejecter, + isResolved, + isRejected, + }; +}; diff --git a/packages/client/src/helpers/withResolvers.ts b/packages/client/src/helpers/withResolvers.ts deleted file mode 100644 index dde818224..000000000 --- a/packages/client/src/helpers/withResolvers.ts +++ /dev/null @@ -1,43 +0,0 @@ -export type PromiseWithResolvers = { - promise: Promise; - resolve: (value: T | PromiseLike) => void; - reject: (reason: any) => void; - isResolved: boolean; - isRejected: boolean; -}; - -/** - * Creates a new promise with resolvers. - * - * Based on: - * - https://github.com/tc39/proposal-promise-with-resolvers/blob/main/polyfills.js - */ -export const promiseWithResolvers = (): PromiseWithResolvers => { - let resolve: (value: T | PromiseLike) => void; - let reject: (reason: any) => void; - const promise = new Promise((_resolve, _reject) => { - resolve = _resolve; - reject = _reject; - }); - - let isResolved = false; - let isRejected = false; - - const resolver = (value: T | PromiseLike) => { - isResolved = true; - resolve(value); - }; - - const rejecter = (reason: any) => { - isRejected = true; - reject(reason); - }; - - return { - promise, - resolve: resolver, - reject: rejecter, - isResolved, - isRejected, - }; -}; diff --git a/packages/react-bindings/CHANGELOG.md b/packages/react-bindings/CHANGELOG.md index a9e488b2b..0c1c26c4f 100644 --- a/packages/react-bindings/CHANGELOG.md +++ b/packages/react-bindings/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [1.2.6](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-bindings-1.2.5...@stream-io/video-react-bindings-1.2.6) (2024-12-03) + +### Dependency Updates + +* `@stream-io/video-client` updated to version `1.11.12` ## [1.2.5](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-bindings-1.2.4...@stream-io/video-react-bindings-1.2.5) (2024-11-29) ### Dependency Updates diff --git a/packages/react-bindings/package.json b/packages/react-bindings/package.json index 7b62e12a4..d879e1fb5 100644 --- a/packages/react-bindings/package.json +++ b/packages/react-bindings/package.json @@ -1,6 +1,6 @@ { "name": "@stream-io/video-react-bindings", - "version": "1.2.5", + "version": "1.2.6", "packageManager": "yarn@3.2.4", "main": "./dist/index.cjs.js", "module": "./dist/index.es.js", diff --git a/packages/react-native-sdk/CHANGELOG.md b/packages/react-native-sdk/CHANGELOG.md index ca6d93abc..a15eb940a 100644 --- a/packages/react-native-sdk/CHANGELOG.md +++ b/packages/react-native-sdk/CHANGELOG.md @@ -2,6 +2,12 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [1.4.8](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-native-sdk-1.4.7...@stream-io/video-react-native-sdk-1.4.8) (2024-12-03) + +### Dependency Updates + +* `@stream-io/video-client` updated to version `1.11.12` +* `@stream-io/video-react-bindings` updated to version `1.2.6` ## [1.4.7](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-native-sdk-1.4.6...@stream-io/video-react-native-sdk-1.4.7) (2024-11-29) ### Dependency Updates diff --git a/packages/react-native-sdk/package.json b/packages/react-native-sdk/package.json index a6dd6d3df..144d5de8c 100644 --- a/packages/react-native-sdk/package.json +++ b/packages/react-native-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@stream-io/video-react-native-sdk", - "version": "1.4.7", + "version": "1.4.8", "packageManager": "yarn@3.2.4", "main": "dist/commonjs/index.js", "module": "dist/module/index.js", diff --git a/packages/react-sdk/CHANGELOG.md b/packages/react-sdk/CHANGELOG.md index 317d18e33..46efb2680 100644 --- a/packages/react-sdk/CHANGELOG.md +++ b/packages/react-sdk/CHANGELOG.md @@ -2,6 +2,12 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [1.7.28](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-sdk-1.7.27...@stream-io/video-react-sdk-1.7.28) (2024-12-03) + +### Dependency Updates + +* `@stream-io/video-client` updated to version `1.11.12` +* `@stream-io/video-react-bindings` updated to version `1.2.6` ## [1.7.27](https://github.com/GetStream/stream-video-js/compare/@stream-io/video-react-sdk-1.7.26...@stream-io/video-react-sdk-1.7.27) (2024-11-29) ### Dependency Updates diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.json index d8223bc5b..2845ee993 100644 --- a/packages/react-sdk/package.json +++ b/packages/react-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@stream-io/video-react-sdk", - "version": "1.7.27", + "version": "1.7.28", "packageManager": "yarn@3.2.4", "main": "./dist/index.cjs.js", "module": "./dist/index.es.js",