heartbeat-ping.js 780 Bytes
const MODULE_NAME = 'KOMODO-SDK-PUSH-TRX.HEARTBEAT-PING';

const uniqid = require('uniqid');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');

/**
 * @param  {WebSocket} ws
 */
module.exports = (ws) => {
    const xid = uniqid();

    if (ws.isAlive === false) {
        logger.info(`${MODULE_NAME} D13AAE47: Terminating dead connection`, {
            apikey: ws.headers.apikey || ws.headers.token,
        });

        ws.terminate();
        return;
    }

    // eslint-disable-next-line no-param-reassign
    ws.isAlive = false;

    logger.debug(`${MODULE_NAME} DBA1B658: Sending ping for heartbeat`, { xid });
    ws.ping(JSON.stringify({
        rid: xid,
        senderName: config.handler_name,
        ts: new Date(),
    }));
};