Search code examples
node.jsamazon-web-servicesamazon-ec2websocketaws-elb

Websocket closing after 60 seconds of being idle while connection node server using AWS ELB


I have one node server running on EC2 instance and client is also running on same EC2 instance, Client open websocket connection to communicate node server, it is working in QA and Dev AWS environment but same web connection is getting close after 60 seconds of being idle in prod environment ,I am running client and node server behind ELB in aws environment.

Client Code:

ws = new WebSocket('ws://localhost:8443');
        
ws.onclose = function () {
    console.log("Websocket connection has been closed.");
    clientObj.emit('LogoffSuccess', 'LogoffSuccessfully');
};
 
ws.onerror=function(event)
{
    console.log(event.data);
};
            
ws.addEventListener('open', function (event) {
    console.log('Websocket connection has been opened');
    ws.send(JSON.stringify(loginCreds));
});
    
    

Node server Code below:

const wss = new WebSocket.Server({ server: app });
const clients = {};
const idMap = {};
    
wss.on(`connection`, ws => {
  const headers = ws.upgradeReq.headers;
  const host = headers.host;
  const key = ws.upgradeReq.headers[`sec-websocket-key`];
    
  ctiServer.on(`responseMessage`, message => {
   clients[message.AgentId].send(JSON.stringify(message));
  });
    
  ws.on(`message`, message => {
    log.info(`Message received. Host: ${host}, Msg: ${message}`);
    if (JSON.parse(message).EventName === `Login`) {
      clients[JSON.parse(message).AgentId] = ws;
      idMap[key] = JSON.parse(message).AgentId;
     }
     ctiServer.processIncomingRequest(message);
  });
    
  ws.on(`close`, () => {
    log.info(`Connection closed. Host: ${host}`);

    const message = {
      EventName: `Logoff`,
      AgentId: idMap[key],
      EventData: {}
    };
        
 });
});

Solution

  • By default, Elastic Load Balancing sets the idle timeout value to 60 seconds. Therefore, if the target doesn't send some data at least every 60 seconds while the request is in flight, the load balancer can close the front-end connection. To ensure that lengthy operations such as file uploads have time to complete, send at least 1 byte of data before each idle timeout period elapses, and increase the length of the idle timeout period as needed.

    https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout

    Note that your interests are best served by periodically sending traffic to keep the connection alive. You can set the idle timeout to up to 4000 seconds in an Application Load Balancer, but you will find that stateful intermediate network infrastructure (firewalls, NAT devices) tends to reset connections before they are actually idle for so long.