From 30abf33ee86b36f2a98014e5b017fa8e466a2107 Mon Sep 17 00:00:00 2001 From: Thomas Hallock Date: Wed, 8 Oct 2025 08:56:39 -0500 Subject: [PATCH] fix: correctly access getSocketIO from dynamic import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dynamic import returns a module namespace object, so we need to access socketServerModule.getSocketIO() rather than treating the module itself as the function. Simplified the wrapper to directly cache and use the module, checking that getSocketIO exists before calling it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- apps/web/src/lib/socket-io.ts | 49 ++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/apps/web/src/lib/socket-io.ts b/apps/web/src/lib/socket-io.ts index b9df24b8..890d74c3 100644 --- a/apps/web/src/lib/socket-io.ts +++ b/apps/web/src/lib/socket-io.ts @@ -6,34 +6,35 @@ import type { Server as SocketIOServerType } from 'socket.io' -// Import the socket server module (this is safe because it's only used in Node.js context) -let socketServer: { getSocketIO: () => SocketIOServerType | null } | null = null - -// Lazy-load the socket server module (only works on server-side) -async function loadSocketServer() { - if (typeof window !== 'undefined') { - // Client-side: return null - return null - } - - if (!socketServer) { - try { - // Dynamic import to avoid bundling issues - socketServer = await import('../../socket-server') - } catch (error) { - console.error('[Socket IO] Failed to load socket server:', error) - return null - } - } - - return socketServer -} +// 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 { - const server = await loadSocketServer() - return server ? server.getSocketIO() : 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 }