Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/libs/Pusher/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EventData<EventName extends string> = {chunk?: string; id?: string; index?:
? PusherEventMap[EventName]
: OnyxUpdatesFromServer);

type EventCallbackError = {type: ValueOf<typeof CONST.ERROR>; data: {code: number}};
type EventCallbackError = {type: ValueOf<typeof CONST.ERROR>; data: {code: number; message?: string}};

type ChunkedDataEvents = {chunks: unknown[]; receivedFinal: boolean};

Expand Down
18 changes: 13 additions & 5 deletions src/libs/PusherConnectionManager.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
Expand Down