From 0c949268e8c696e90f350b2e0eb64fca7ff8c20a Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 11 Sep 2020 11:44:25 -0700 Subject: [PATCH] add reconnect logic for Pusher --- src/lib/API.js | 14 ++++++++++++++ src/lib/Pusher/pusher.js | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lib/API.js b/src/lib/API.js index e9bb87454281..01d92b384798 100644 --- a/src/lib/API.js +++ b/src/lib/API.js @@ -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 @@ -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}`)); }); @@ -311,6 +322,9 @@ Pusher.registerSocketEventCallback((eventName, data) => { isCurrentlyOffline = true; } break; + case 'error': + reconnectToPusher(); + break; default: break; } diff --git a/src/lib/Pusher/pusher.js b/src/lib/Pusher/pusher.js index 97278d0f0c98..7c9f209fb148 100644 --- a/src/lib/Pusher/pusher.js +++ b/src/lib/Pusher/pusher.js @@ -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 @@ -376,4 +384,5 @@ export { registerSocketEventCallback, registerCustomAuthorizer, disconnect, + reconnect, };