Skip to content
Draft
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
36 changes: 35 additions & 1 deletion packages/client/src/coordinator/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export class StableWSConnection {
totalFailures: number;
ws?: WebSocket;
wsID: number;

client: StreamClient;

constructor(client: StreamClient) {
Expand Down Expand Up @@ -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...
*
Expand Down Expand Up @@ -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`);
Expand Down Expand Up @@ -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;
}
Expand All @@ -344,6 +377,7 @@ export class StableWSConnection {
options: { interval?: number; refreshToken?: boolean } = {},
): Promise<void> {
this._log('_reconnect() - Initiating the reconnect');
this._probeDNS();

// only allow 1 connection at the time
if (this.isConnecting || this.isHealthy) {
Expand Down
2 changes: 1 addition & 1 deletion sample-apps/react/react-dogfood/helpers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
3 changes: 1 addition & 2 deletions sample-apps/react/react-dogfood/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading