@@ -99,21 +99,13 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
9999 workflowDeleted ?: ( data : any ) => void
100100 } > ( { } )
101101
102- // Initialize socket when user is available
102+ // Initialize socket when user is available - only once per session
103103 useEffect ( ( ) => {
104104 if ( ! user ?. id ) return
105105
106- // Prevent duplicate connections - disconnect existing socket first
107- if ( socket ) {
108- logger . info ( 'Disconnecting existing socket before creating new one' )
109- socket . disconnect ( )
110- setSocket ( null )
111- setIsConnected ( false )
112- }
113-
114- // Prevent multiple simultaneous initialization attempts
115- if ( isConnecting ) {
116- logger . info ( 'Socket initialization already in progress, skipping' )
106+ // Only initialize if we don't have a socket and aren't already connecting
107+ if ( socket || isConnecting ) {
108+ logger . info ( 'Socket already exists or is connecting, skipping initialization' )
117109 return
118110 }
119111
@@ -296,16 +288,8 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
296288 // Start the socket initialization
297289 initializeSocket ( )
298290
299- // Cleanup on unmount or user change
291+ // Cleanup on unmount only (not on user change since socket is session-level)
300292 return ( ) => {
301- if ( socket ) {
302- logger . info ( 'Cleaning up socket connection' )
303- socket . disconnect ( )
304- setSocket ( null )
305- setIsConnected ( false )
306- setIsConnecting ( false )
307- }
308-
309293 positionUpdateTimeouts . current . forEach ( ( timeoutId ) => {
310294 clearTimeout ( timeoutId )
311295 } )
@@ -314,6 +298,16 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
314298 }
315299 } , [ user ?. id ] )
316300
301+ // Cleanup socket on component unmount
302+ useEffect ( ( ) => {
303+ return ( ) => {
304+ if ( socket ) {
305+ logger . info ( 'Cleaning up socket connection on unmount' )
306+ socket . disconnect ( )
307+ }
308+ }
309+ } , [ ] )
310+
317311 // Join workflow room
318312 const joinWorkflow = useCallback (
319313 ( workflowId : string ) => {
0 commit comments