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 @@ -42,6 +42,9 @@ const socket = React.useRef(null); const heartbeatTimeoutID = React.useRef(); + const previousIdentitySearchAuthMessage = + React.useRef(identitySearchAuthMessage); + const stopHeartbeatTimeout = React.useCallback(() => { if (heartbeatTimeoutID.current) { clearTimeout(heartbeatTimeoutID.current); @@ -65,6 +68,22 @@ }, [getIdentitySearchAuthMessage]); React.useEffect(() => { + const isSocketActive = + socket.current?.readyState === WebSocket.OPEN || + socket.current?.readyState === WebSocket.CONNECTING; + + const identitySearchAuthMessageChanged = + identitySearchAuthMessage !== previousIdentitySearchAuthMessage.current; + previousIdentitySearchAuthMessage.current = identitySearchAuthMessage; + + if ( + (!identitySearchAuthMessage || identitySearchAuthMessageChanged) && + isSocketActive + ) { + socket.current?.close(); + return; + } + if (connected || !identitySearchAuthMessage) { return; }