Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reconnect logic for Pusher #475

Merged
merged 1 commit into from
Sep 11, 2020
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
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,
};