Node.js WebSocket reconnect loop hangs on `close` event

Building a custom listener for the Notification API in Node.js. The ws library handles the initial connection, but the reconnection logic gets stuck in a loop when the server sends a 1001 close code. The ws.on('close') handler triggers, calls connect(), but the new socket never opens. Tried adding a 5s delay before reconnecting, no change. Here’s the :

ws.on('close', (code, reason) => {
 console.log('Closed:', code);
 setTimeout(connect, 5000);
});

Why isn’t the socket reopening?