From cb915883104bf3814a99c4a40d4945750b6450a4 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 16 Jan 2025 13:18:57 -0700 Subject: [PATCH 1/3] Manually reconnect to pusher when there is a 1006 error message --- src/libs/Pusher/pusher.ts | 2 +- src/libs/PusherConnectionManager.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libs/Pusher/pusher.ts b/src/libs/Pusher/pusher.ts index 1dd1628e53d2..1b7eb8664939 100644 --- a/src/libs/Pusher/pusher.ts +++ b/src/libs/Pusher/pusher.ts @@ -40,7 +40,7 @@ type EventData = {chunk?: string; id?: string; index?: ? PusherEventMap[EventName] : OnyxUpdatesFromServer); -type EventCallbackError = {type: ValueOf; data: {code: number}}; +type EventCallbackError = {type: ValueOf; data: {code: number; message?: string}}; type ChunkedDataEvents = {chunks: unknown[]; receivedFinal: boolean}; diff --git a/src/libs/PusherConnectionManager.ts b/src/libs/PusherConnectionManager.ts index 597670d3f5ad..bc2b135f21ef 100644 --- a/src/libs/PusherConnectionManager.ts +++ b/src/libs/PusherConnectionManager.ts @@ -25,10 +25,18 @@ function init() { if (error && 'type' in error) { const errorType = error?.type; const code = error?.data?.code; + const errorMessage = error?.data?.message || ''; if (errorType === CONST.ERROR.PUSHER_ERROR && code === 1006) { // 1006 code happens when a websocket connection is closed. There may or may not be a reason attached indicating why the connection was closed. // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5 Log.hmmm('[PusherConnectionManager] Channels Error 1006', {error}); + + // The 1006 errors don't always have a message, but when they do, it seems that it prevents the pusher client from reconnecting. + // On the advice from Pusher directly, they suggested to manually reconnect in those scenarios. + if (errorMessage) { + Log.hmmm('[PusherConnectionManager] Channels Error 1006 message', {errorMessage}); + Pusher.reconnect(); + } } else if (errorType === CONST.ERROR.PUSHER_ERROR && code === 4201) { // This means the connection was closed because Pusher did not receive a reply from the client when it pinged them for a response // https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol/#4200-4299 From 63e9f63157ab48ae71a05588798f4c66f75c6df8 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 16 Jan 2025 13:25:46 -0700 Subject: [PATCH 2/3] Fix lint --- src/libs/PusherConnectionManager.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libs/PusherConnectionManager.ts b/src/libs/PusherConnectionManager.ts index bc2b135f21ef..0e8ceed76f73 100644 --- a/src/libs/PusherConnectionManager.ts +++ b/src/libs/PusherConnectionManager.ts @@ -1,9 +1,9 @@ import type {ChannelAuthorizationCallback} from 'pusher-js/with-encryption'; import CONST from '@src/CONST'; -import * as Session from './actions/Session'; +import {authenticatePusher} from './actions/Session'; import Log from './Log'; import type {SocketEventName} from './Pusher/library/types'; -import * as Pusher from './Pusher/pusher'; +import {reconnect, registerCustomAuthorizer, registerSocketEventCallback} from './Pusher/pusher'; import type {EventCallbackError, States} from './Pusher/pusher'; function init() { @@ -13,13 +13,13 @@ function init() { * current valid token to generate the signed auth response * needed to subscribe to Pusher channels. */ - Pusher.registerCustomAuthorizer((channel) => ({ + registerCustomAuthorizer((channel) => ({ authorize: (socketId: string, callback: ChannelAuthorizationCallback) => { - Session.authenticatePusher(socketId, channel.name, callback); + authenticatePusher(socketId, channel.name, callback); }, })); - Pusher.registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => { + registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => { switch (eventName) { case 'error': { if (error && 'type' in error) { @@ -35,7 +35,7 @@ function init() { // On the advice from Pusher directly, they suggested to manually reconnect in those scenarios. if (errorMessage) { Log.hmmm('[PusherConnectionManager] Channels Error 1006 message', {errorMessage}); - Pusher.reconnect(); + reconnect(); } } else if (errorType === CONST.ERROR.PUSHER_ERROR && code === 4201) { // This means the connection was closed because Pusher did not receive a reply from the client when it pinged them for a response From cee2970fd07c5128636959540de099ab42634687 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 16 Jan 2025 13:26:04 -0700 Subject: [PATCH 3/3] Use null operator --- src/libs/PusherConnectionManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/PusherConnectionManager.ts b/src/libs/PusherConnectionManager.ts index 0e8ceed76f73..69ffa8339f5c 100644 --- a/src/libs/PusherConnectionManager.ts +++ b/src/libs/PusherConnectionManager.ts @@ -25,7 +25,7 @@ function init() { if (error && 'type' in error) { const errorType = error?.type; const code = error?.data?.code; - const errorMessage = error?.data?.message || ''; + const errorMessage = error?.data?.message ?? ''; if (errorType === CONST.ERROR.PUSHER_ERROR && code === 1006) { // 1006 code happens when a websocket connection is closed. There may or may not be a reason attached indicating why the connection was closed. // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5