diff --git a/lib/identity-search/identity-search-context.js b/lib/identity-search/identity-search-context.js --- a/lib/identity-search/identity-search-context.js +++ b/lib/identity-search/identity-search-context.js @@ -38,6 +38,8 @@ const socket = React.useRef(null); const heartbeatTimeoutID = React.useRef(); + const previousAuthMessage = React.useRef(authMessage); + const stopHeartbeatTimeout = React.useCallback(() => { if (heartbeatTimeoutID.current) { clearTimeout(heartbeatTimeoutID.current); @@ -54,6 +56,18 @@ }, [stopHeartbeatTimeout]); React.useEffect(() => { + const isSocketActive = + socket.current?.readyState === WebSocket.OPEN || + socket.current?.readyState === WebSocket.CONNECTING; + + const authMessageChanged = authMessage !== previousAuthMessage.current; + previousAuthMessage.current = authMessage; + + if ((!authMessage || authMessageChanged) && isSocketActive) { + socket.current?.close(); + return; + } + if (connected || !authMessage) { return; }