feature/IO-3303-Socket-IO-Optimization-Auto-Add-Watchers-Gate - SocketIO Optimization / Auto Add Watchers Gate

This commit is contained in:
Dave Richer
2025-07-14 18:42:27 -04:00
parent fbd6766dcd
commit 72091e9eae
5 changed files with 182 additions and 98 deletions

View File

@@ -16,7 +16,7 @@ import {
import { useMutation } from "@apollo/client";
import { useTranslation } from "react-i18next";
import { useSplitTreatments } from "@splitsoftware/splitio-react";
import { SocketContext, INITIAL_NOTIFICATIONS } from "./useSocket.js";
import { INITIAL_NOTIFICATIONS, SocketContext } from "./useSocket.js";
/**
* Socket Provider - Scenario Notifications / Web Socket related items
@@ -31,6 +31,7 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
const socketRef = useRef(null);
const [clientId, setClientId] = useState(null);
const [isConnected, setIsConnected] = useState(false);
const [socketInitialized, setSocketInitialized] = useState(false);
const notification = useNotification();
const userAssociationId = bodyshop?.associations?.[0]?.id;
const { t } = useTranslation();
@@ -146,6 +147,13 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
onError: (err) => console.error("MARK_ALL_NOTIFICATIONS_READ error:", err)
});
const checkAndReconnect = () => {
if (socketRef.current && !socketRef.current.connected) {
console.log("Attempting manual reconnect due to event trigger");
socketRef.current.connect();
}
};
useEffect(() => {
const initializeSocket = async (token) => {
if (!bodyshop || !bodyshop.id || socketRef.current) return;
@@ -157,10 +165,14 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
auth: { token, bodyshopId: bodyshop.id },
reconnectionAttempts: Infinity,
reconnectionDelay: 2000,
reconnectionDelayMax: 10000
reconnectionDelayMax: 60000,
randomizationFactor: 0.5,
transports: ["websocket", "polling"], // Add this to prefer WebSocket with polling fallback
rememberUpgrade: true
});
socketRef.current = socketInstance;
setSocketInitialized(true);
const handleBodyshopMessage = (message) => {
if (!message || !message.type) return;
@@ -469,6 +481,57 @@ const SocketProvider = ({ children, bodyshop, navigate, currentUser }) => {
t
]);
useEffect(() => {
if (!socketInitialized) return;
const onVisibilityChange = () => {
if (document.visibilityState === "visible") {
checkAndReconnect();
}
};
const onFocus = () => {
checkAndReconnect();
};
const onOnline = () => {
checkAndReconnect();
};
const onPageShow = (event) => {
if (event.persisted) {
checkAndReconnect();
}
};
document.addEventListener("visibilitychange", onVisibilityChange);
window.addEventListener("focus", onFocus);
window.addEventListener("online", onOnline);
window.addEventListener("pageshow", onPageShow);
// Sleep/wake detection using timer
let lastTime = Date.now();
const intervalMs = 1000; // Check every second
const thresholdMs = 2000; // If more than 2 seconds elapsed, assume sleep/wake
const sleepCheckInterval = setInterval(() => {
const currentTime = Date.now();
if (currentTime > lastTime + intervalMs + thresholdMs) {
console.log("Detected potential wake from sleep/hibernate");
checkAndReconnect();
}
lastTime = currentTime;
}, intervalMs);
return () => {
document.removeEventListener("visibilitychange", onVisibilityChange);
window.removeEventListener("focus", onFocus);
window.removeEventListener("online", onOnline);
window.removeEventListener("pageshow", onPageShow);
clearInterval(sleepCheckInterval);
};
}, [socketInitialized]);
return (
<SocketContext.Provider
value={{