diff --git a/packages/client/src/coordinator/connection/connection.ts b/packages/client/src/coordinator/connection/connection.ts index 8027cf2da2..e0928c017e 100644 --- a/packages/client/src/coordinator/connection/connection.ts +++ b/packages/client/src/coordinator/connection/connection.ts @@ -60,7 +60,6 @@ export class StableWSConnection { totalFailures: number; ws?: WebSocket; wsID: number; - client: StreamClient; constructor(client: StreamClient) { @@ -209,6 +208,38 @@ export class StableWSConnection { return `${this.client.wsBaseURL}/connect?${params.toString()}`; }; + /** + * _probeDNS - Fire-and-forget DNS diagnostic probes. + * Logs which domains resolve and which fail when the WS connection fails. + */ + _probeDNS = () => { + const token = this.client._getToken(); + const authHeaders = { + 'stream-auth-type': this.client.getAuthType(), + ...(token ? { Authorization: token } : {}), + }; + const probes: Array<{ + url: string; + method?: string; + headers?: HeadersInit; + }> = [ + { url: `https://hint.stream-io-video.com/`, method: 'HEAD' }, + { + url: `https://chat.stream-io-api.com/hi?api_key=${this.client.key}`, + headers: authHeaders, + }, + { + url: `https://video.stream-io-api.com/hi?api_key=${this.client.key}`, + headers: authHeaders, + }, + ]; + for (const { url, method, headers } of probes) { + fetch(url, { method: method ?? 'GET', headers }) + .then(() => this._log(`[DNS-PROBE] ${url}: OK`)) + .catch((e) => this._log(`[DNS-PROBE] ${url}: FAILED - ${e.message}`)); + } + }; + /** * disconnect - Disconnect the connection and doesn't recover... * @@ -285,6 +316,7 @@ export class StableWSConnection { async _connect() { if (this.isConnecting) return; // ignore _connect if it's currently trying to connect this.isConnecting = true; + this._probeDNS(); let isTokenReady = false; try { this._log(`_connect() - waiting for token`); @@ -327,6 +359,7 @@ export class StableWSConnection { this.isConnecting = false; // @ts-expect-error type issue this._log(`_connect() - Error - `, err); + this._probeDNS(); this.client.rejectConnectionId?.(err); throw err; } @@ -344,6 +377,7 @@ export class StableWSConnection { options: { interval?: number; refreshToken?: boolean } = {}, ): Promise { this._log('_reconnect() - Initiating the reconnect'); + this._probeDNS(); // only allow 1 connection at the time if (this.isConnecting || this.isHealthy) { diff --git a/sample-apps/react/react-dogfood/helpers/client.ts b/sample-apps/react/react-dogfood/helpers/client.ts index 9c9a4740be..adb616293a 100644 --- a/sample-apps/react/react-dogfood/helpers/client.ts +++ b/sample-apps/react/react-dogfood/helpers/client.ts @@ -83,7 +83,7 @@ export const createTokenProvider = ( const params = new URLSearchParams({ user_id: userId || '!anon', environment, - exp: String(4 * 60 * 60), // 4 hours + exp: String(60), } satisfies CreateJwtTokenRequest); const res = await fetch(`${basePath}/api/auth/create-token?${params}`); diff --git a/sample-apps/react/react-dogfood/helpers/jwt.ts b/sample-apps/react/react-dogfood/helpers/jwt.ts index c8a33af41f..bddfe089f8 100644 --- a/sample-apps/react/react-dogfood/helpers/jwt.ts +++ b/sample-apps/react/react-dogfood/helpers/jwt.ts @@ -5,8 +5,7 @@ import { decodeBase64 } from 'stream-chat'; * The maximum validity of a token, in seconds. * Defaults to 7 days. */ -export const maxTokenValidityInSeconds: number = - Number(process.env.MAX_TOKEN_EXP_IN_SECONDS) || 7 * 24 * 60 * 60; // 7 days +export const maxTokenValidityInSeconds: number = 60; export const createToken = async ( userId: string,