All files / web/src/lib socket-io.ts

0% Statements 0/40
0% Branches 0/1
0% Functions 0/1
0% Lines 0/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                                 
/**
 * Socket.IO server instance accessor for API routes
 * This module provides a way for API routes to access the socket.io server
 * to broadcast real-time updates.
 */

import type { Server as SocketIOServerType } from 'socket.io'

// Cache for the socket server module
let socketServerModule: any = null

/**
 * Get the socket.io server instance
 * Returns null if not initialized or if called on client-side
 */
export async function getSocketIO(): Promise<SocketIOServerType | null> {
  // Client-side: return null
  if (typeof window !== 'undefined') {
    return null
  }

  // Lazy-load the socket server module on first call
  if (!socketServerModule) {
    try {
      // Dynamic import to avoid bundling issues
      socketServerModule = await import('../socket-server')
    } catch (error) {
      console.error('[Socket IO] Failed to load socket server:', error)
      return null
    }
  }

  // Call the exported getSocketIO function from the module
  if (socketServerModule && typeof socketServerModule.getSocketIO === 'function') {
    return socketServerModule.getSocketIO()
  }

  console.warn('[Socket IO] getSocketIO function not found in socket-server module')
  return null
}