Skip to content

Commit a81e6e9

Browse files
committed
chore: log SSE connect/disconnect and add SSE diagnostic endpoint
1 parent 58b913d commit a81e6e9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

server/src/webhook.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,36 @@ export function registerWebhookRoutes(app: express.Application) {
183183
set.add(res);
184184
sseClients.set(userPath, set);
185185

186+
// Log connection for debugging (shows remote address and total clients for the path)
187+
try {
188+
const remote = (req.ip || (req.socket && (req.socket.remoteAddress || req.socket.remoteFamily)) || 'unknown');
189+
console.log(`SSE connected: path=${userPath} remote=${remote} clients=${set.size}`);
190+
} catch (err) {
191+
// ignore logging errors
192+
}
193+
186194
req.on('close', () => {
187195
const clients = sseClients.get(userPath);
188196
if (clients) {
189197
clients.delete(res);
190198
if (clients.size === 0) sseClients.delete(userPath);
191199
}
200+
try {
201+
const remote = (req.ip || (req.socket && (req.socket.remoteAddress || req.socket.remoteFamily)) || 'unknown');
202+
console.log(`SSE disconnected: path=${userPath} remote=${remote} remaining=${sseClients.get(userPath)?.size || 0}`);
203+
} catch (err) {
204+
// ignore
205+
}
192206
});
193207
});
194208

209+
// Diagnostic: number of SSE clients for a given webhook path
210+
app.get('/__diag/webhook-sse/:userPath', (req: Request, res: Response) => {
211+
const userPath = req.params.userPath;
212+
const clients = sseClients.get(userPath) || new Set<Response>();
213+
return res.json({ path: userPath, clients: clients.size, keys: Array.from(sseClients.keys()) });
214+
});
215+
195216
// Diagnostic endpoint to inspect last webhook request for a path
196217
app.get('/__diag/webhook/:userPath', (req: Request, res: Response) => {
197218
const userPath = req.params.userPath;

0 commit comments

Comments
 (0)