Skip to content

Commit

Permalink
remove auto socket resubscription from SocketsService, was causing lo…
Browse files Browse the repository at this point in the history
…ts of lag/duplicated sockets
  • Loading branch information
jacksontromero committed Oct 21, 2023
1 parent f319dd8 commit 7b885d8
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions client/src/services/SocketsService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import Cookies from 'universal-cookie';

const cookies = new Cookies();

let socket = null;
const subscriptions = {};

let socket;
const SOCKET_URL = process.env.REACT_APP_PROTOCOL + '://' + process.env.REACT_APP_DOMAIN;
const SOCKET_PATH = process.env.REACT_APP_SOCKET_PATH;

Expand All @@ -27,9 +25,6 @@ export const socketSubscribeTo = (emission, callback) => {
initiateSocket();
}

// Add the event and callback to our list of subscriptions
subscriptions[emission] = callback;

socket.on(emission, (data) => {
callback(data);
});
Expand All @@ -40,29 +35,5 @@ export const socketUnsubscribeFrom = (emission) => {
return;
}

if (subscriptions.hasOwnProperty(emission)) {
delete subscriptions[emission];
}

socket.off(emission);
};

// Function to resubscribe to all events
const resubscribeAll = () => {
for (const [emission, callback] of Object.entries(subscriptions)) {
socketSubscribeTo(emission, callback);
}
};

// Visibility change listener
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
// The page is visible again
if (!socket || socket.readyState !== WebSocket.OPEN) {
// If the socket is not open, reconnect and resubscribe to all events
socket = null;
initiateSocket();
resubscribeAll();
}
}
});

0 comments on commit 7b885d8

Please sign in to comment.