MCPcopy
hub / github.com/simstudioai/sim / initializeSocket

Function initializeSocket

apps/sim/app/workspace/providers/socket-provider.tsx:366–768  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

364 setIsConnecting(true)
365
366 const initializeSocket = async () => {
367 try {
368 const { io } = await import('socket.io-client')
369 const socketUrl = getSocketUrl()
370
371 logger.info('Attempting to connect to Socket.IO server', {
372 url: socketUrl,
373 userId: user?.id || 'no-user',
374 timestamp: new Date().toISOString(),
375 })
376
377 const socketInstance = io(socketUrl, {
378 transports: ['websocket', 'polling'],
379 withCredentials: true,
380 reconnectionAttempts: Number.POSITIVE_INFINITY,
381 reconnectionDelay: 1000,
382 reconnectionDelayMax: 30000,
383 timeout: 10000,
384 auth: async (cb) => {
385 try {
386 const freshToken = await generateSocketToken()
387 cb({ token: freshToken })
388 } catch (error) {
389 logger.error('Failed to generate fresh token for connection:', error)
390 if (error instanceof Error && error.message === 'Authentication required') {
391 // True auth failure - pass null token, server will reject with "Authentication required"
392 cb({ token: null })
393 }
394 // For server errors, don't call cb - connection will timeout and Socket.IO will retry
395 }
396 },
397 })
398
399 socketInstance.on('connect', () => {
400 setIsConnected(true)
401 setIsConnecting(false)
402 setIsReconnecting(false)
403 authRetryAttemptsRef.current = 0
404 clearAuthRetryTimeout()
405 setCurrentSocketId(socketInstance.id ?? null)
406 logger.info('Socket connected successfully', {
407 socketId: socketInstance.id,
408 connected: socketInstance.connected,
409 transport: socketInstance.io.engine?.transport?.name,
410 })
411 executeJoinCommands(joinControllerRef.current.setConnected(true))
412 })
413
414 socketInstance.on('disconnect', (reason) => {
415 setIsConnected(false)
416 setIsConnecting(false)
417 setIsRetryingWorkflowJoin(false)
418 setCurrentSocketId(null)
419 executeJoinCommands(joinControllerRef.current.setConnected(false))
420 clearJoinedWorkflowState(false)
421
422 if (socketInstance.active) {
423 setIsReconnecting(true)

Callers 1

SocketProviderFunction · 0.85

Calls 15

getSocketUrlFunction · 0.90
backoffWithJitterFunction · 0.90
generateSocketTokenFunction · 0.85
rehydrateWorkflowStoresFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
onMethod · 0.80
setConnectedMethod · 0.80
handleJoinSuccessMethod · 0.80
debugMethod · 0.80
handleJoinErrorMethod · 0.80
handleWorkflowDeletedMethod · 0.80

Tested by

no test coverage detected