Skip to content

Commit 0efbc10

Browse files
authored
Merge pull request #55369 from Expensify/tgolen-reconnect-pusher
[No QA] Manually reconnect to pusher when there is a 1006 error message
2 parents 12116fb + cee2970 commit 0efbc10

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/libs/Pusher/pusher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type EventData<EventName extends string> = {chunk?: string; id?: string; index?:
4040
? PusherEventMap[EventName]
4141
: OnyxUpdatesFromServer);
4242

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

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

src/libs/PusherConnectionManager.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {ChannelAuthorizationCallback} from 'pusher-js/with-encryption';
22
import CONST from '@src/CONST';
3-
import * as Session from './actions/Session';
3+
import {authenticatePusher} from './actions/Session';
44
import Log from './Log';
55
import type {SocketEventName} from './Pusher/library/types';
6-
import * as Pusher from './Pusher/pusher';
6+
import {reconnect, registerCustomAuthorizer, registerSocketEventCallback} from './Pusher/pusher';
77
import type {EventCallbackError, States} from './Pusher/pusher';
88

99
function init() {
@@ -13,22 +13,30 @@ function init() {
1313
* current valid token to generate the signed auth response
1414
* needed to subscribe to Pusher channels.
1515
*/
16-
Pusher.registerCustomAuthorizer((channel) => ({
16+
registerCustomAuthorizer((channel) => ({
1717
authorize: (socketId: string, callback: ChannelAuthorizationCallback) => {
18-
Session.authenticatePusher(socketId, channel.name, callback);
18+
authenticatePusher(socketId, channel.name, callback);
1919
},
2020
}));
2121

22-
Pusher.registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => {
22+
registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => {
2323
switch (eventName) {
2424
case 'error': {
2525
if (error && 'type' in error) {
2626
const errorType = error?.type;
2727
const code = error?.data?.code;
28+
const errorMessage = error?.data?.message ?? '';
2829
if (errorType === CONST.ERROR.PUSHER_ERROR && code === 1006) {
2930
// 1006 code happens when a websocket connection is closed. There may or may not be a reason attached indicating why the connection was closed.
3031
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5
3132
Log.hmmm('[PusherConnectionManager] Channels Error 1006', {error});
33+
34+
// The 1006 errors don't always have a message, but when they do, it seems that it prevents the pusher client from reconnecting.
35+
// On the advice from Pusher directly, they suggested to manually reconnect in those scenarios.
36+
if (errorMessage) {
37+
Log.hmmm('[PusherConnectionManager] Channels Error 1006 message', {errorMessage});
38+
reconnect();
39+
}
3240
} else if (errorType === CONST.ERROR.PUSHER_ERROR && code === 4201) {
3341
// This means the connection was closed because Pusher did not receive a reply from the client when it pinged them for a response
3442
// https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol/#4200-4299

0 commit comments

Comments
 (0)