{"record":{"id":"dd78a144857ce920","repo":"louislam/dockge","slug":"socket-client-not-connected-for-endpoint","errorCode":null,"errorMessage":"Socket client not connected for endpoint: ","messagePattern":"Socket client not connected for endpoint: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"backend/agent-manager.ts","lineNumber":275,"sourceCode":"            // Maybe the request is too quick, the socket is not connected yet, check firstConnectTime\n            // If it is within 10 seconds, we should apply retry logic here\n            let diff = dayjs().diff(this.firstConnectTime, \"second\");\n            log.debug(\"agent-manager\", endpoint + \": diff: \" + diff);\n            let ok = false;\n            while (diff < 10) {\n                if (client.connected && this.agentLoggedInList[endpoint]) {\n                    log.debug(\"agent-manager\", `${endpoint}: Connected & Logged in`);\n                    ok = true;\n                    break;\n                }\n                log.debug(\"agent-manager\", endpoint + \": not ready yet, retrying in 1 second...\");\n                await sleep(1000);\n                diff = dayjs().diff(this.firstConnectTime, \"second\");\n            }\n\n            if (!ok) {\n                log.error(\"agent-manager\", `${endpoint}: Socket client not connected`);\n                throw new Error(\"Socket client not connected for endpoint: \" + endpoint);\n            }\n        }\n\n        client.emit(\"agent\", endpoint, eventName, ...args);\n    }\n\n    emitToAllEndpoints(eventName: string, ...args : unknown[]) {\n        log.debug(\"agent-manager\", \"Emitting event to all endpoints\");\n        for (let endpoint in this.agentSocketList) {\n            this.emitToEndpoint(endpoint, eventName, ...args).catch((e) => {\n                log.warn(\"agent-manager\", e.message);\n            });\n        }\n    }\n\n    async sendAgentList() {\n        let list = await Agent.getAgentList();\n        let result : Record<string, LooseObject> = {};","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/louislam/dockge/blob/f809ae192b571944ad773e9866d3e67064ae8043/backend/agent-manager.ts#L257-L293","documentation":"After finding the socket client, emitToEndpoint() checks client.connected and agentLoggedInList[endpoint]. If the socket is not connected or the agent login has not completed, it retries while within ~10 seconds of firstConnectTime; if still not ready it throws 'Socket client not connected for endpoint: <endpoint>'.","triggerScenarios":"emitToEndpoint called while the agent socket is disconnected (network drop, agent Dockge instance down/restarting) or before the agent 'login' handshake finished, and the retry window (firstConnectTime + 10s) elapsed without the socket becoming ready.","commonSituations":"Remote Dockge agent offline or restarting during a deploy/stack command; wrong credentials so the agent never reaches logged-in state; firewall/network interruption; emitting immediately after page load before the connection+login completes past the 10s grace window.","solutions":["Check the agent instance is up and reachable (curl its URL/port) and wait for 'connected' status before emitting","Reconnect the agent: call connect() again or recreate the agent entry, since the client may have permanently disconnected after the retry window","Verify agent credentials so the login step completes and agentLoggedInList[endpoint] becomes true"],"exampleFix":"// before\nawait agentManager.emitToEndpoint(endpoint, \"startStack\", name);\n// after\ntry {\n    await agentManager.emitToEndpoint(endpoint, \"startStack\", name);\n} catch (e) {\n    if (String(e.message).startsWith(\"Socket client not connected\")) {\n        await new Promise(r => setTimeout(r, 3000)); // or trigger reconnect\n        await agentManager.emitToEndpoint(endpoint, \"startStack\", name);\n    } else {\n        throw e;\n    }\n}","handlingStrategy":"retry","validationCode":"// check agent reachability before emitting\nconst res = await fetch(`http://${endpoint}`).catch(() => null);\nif (!res) throw new Error(`Agent ${endpoint} unreachable`);","typeGuard":null,"tryCatchPattern":"try {\n    await agentManager.emitToEndpoint(endpoint, eventName, ...args);\n} catch (e) {\n    if (String(e.message).startsWith(\"Socket client not connected\")) {\n        await sleep(3000); // or wait for agentStatus 'connected' event\n        await agentManager.emitToEndpoint(endpoint, eventName, ...args);\n    } else throw e;\n}","preventionTips":["Wait for the connected/logged-in agent status event before emitting","Keep agent instances running and reachable; monitor health","Fix credentials so the agent login completes","Avoid emitting within flaky-network windows; rely on retry with backoff"],"tags":["socket","connection","timeout","agent-manager"],"backgroundTag":"socket-not-connected","analyzedSha":"f809ae192b571944ad773e9866d3e67064ae8043","analyzedAt":"2026-08-31T19:13:50.919Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}