Skip to content

Commit

Permalink
Merge pull request #475 from Expensify/marcaaron-fixPusher
Browse files Browse the repository at this point in the history
Add reconnect logic for Pusher
  • Loading branch information
AndrewGable authored Sep 11, 2020
2 parents 372f7dc + 0c94926 commit df8cfb5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ function processNetworkRequestQueue() {
// Process our write queue very often
setInterval(processNetworkRequestQueue, 1000);

/**
* Pusher.reconnect() calls disconnect and connect on the
* Pusher socket. In some cases, the authorizer might fail
* or an error will be returned due to an out of date authToken.
* Reconnect will preserve our existing subscriptions and retry
* connecting until it succeeds. We're throttling this call so
* that we retry as few times as possible.
*/
const reconnectToPusher = _.throttle(Pusher.reconnect, 1000);

/**
* When authTokens expire they will automatically be refreshed.
* The authorizer helps make sure that we are always passing the
Expand All @@ -284,6 +294,7 @@ Pusher.registerCustomAuthorizer((channel, {authEndpoint}) => ({
.then(authResponse => authResponse.json())
.then(data => callback(null, data))
.catch((err) => {
reconnectToPusher();
console.debug('[Network] Failed to authorize Pusher');
callback(new Error(`Error calling auth endpoint: ${err}`));
});
Expand Down Expand Up @@ -311,6 +322,9 @@ Pusher.registerSocketEventCallback((eventName, data) => {
isCurrentlyOffline = true;
}
break;
case 'error':
reconnectToPusher();
break;
default:
break;
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/Pusher/pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ function disconnect() {
socket = null;
}

/**
* Disconnect and Re-Connect Pusher
*/
function reconnect() {
socket.disconnect();
socket.connect();
}

if (window) {
/**
* Pusher socket for debugging purposes
Expand All @@ -376,4 +384,5 @@ export {
registerSocketEventCallback,
registerCustomAuthorizer,
disconnect,
reconnect,
};

0 comments on commit df8cfb5

Please sign in to comment.