Skip to content

Commit

Permalink
Reconnection Logic (#2743)
Browse files Browse the repository at this point in the history
* reconnection logic

* prettier state.js

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
  • Loading branch information
Yummy-Yums and masenf committed Mar 1, 2024
1 parent 93fc269 commit 7725c48
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions reflex/.templates/web/utils/state.js
Expand Up @@ -325,6 +325,17 @@ export const connect = async (
autoUnref: false,
});

function checkVisibility() {
if (document.visibilityState === "visible") {
if (!socket.current.connected) {
console.log("Socket is disconnected, attempting to reconnect ");
socket.current.connect();
} else {
console.log("Socket is reconnected ");
}
}
}

// Once the socket is open, hydrate the page.
socket.current.on("connect", () => {
setConnectErrors([]);
Expand All @@ -345,6 +356,8 @@ export const connect = async (
queueEvents(update.events, socket);
}
});

document.addEventListener("visibilitychange", checkVisibility);
};

/**
Expand Down

0 comments on commit 7725c48

Please sign in to comment.