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..69ffa8339f5c 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,22 +13,30 @@ 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) { 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}); + 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