Skip to content

Commit

Permalink
feat: implement WebSocket heartbeat mechanism for connection management
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinkrustev committed Feb 25, 2025
1 parent fde9f73 commit 7a7b9e1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mono-log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ const init = async () => {

const wss = new WebSocketServer({ server: server.listener });

function heartbeat() {
this.isAlive = true;
}

wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
});

const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);

wss.on('close', function close() {
clearInterval(interval);
});

const logMessages = [];

const udpServer = dgram.createSocket('udp4');
Expand Down

0 comments on commit 7a7b9e1

Please sign in to comment.