title: Implement heartbeat for api gateway websockets
body: <p>I am building a chatroom app using nodejs + api gateway websockets. I understand that each websocket connection can only be idle for up to 29 seconds before it gets disconnected so I wrote a &quot;hearbeat&quot; function where the websocket connection pings the client every 20 seconds to keep the connection alive and not idle and I have the server send a response. Despite my heartbeat function, my websocket connections are still automatically closing every 29 seconds. Can someone let me know what about my implementation is incorrect?</p>
  <pre><code>Client:
  
     const ws = new WebSocket(myWebsocketURL);
  
      ws.onopen = () =&gt; {
        setTimeout(() =&gt; {
          ws.send(&quot;ping&quot;);
        }, 20000);
      };
  
      ws.onclose = () =&gt; {
        // handle close
      };
  
  Server:
  
     const api = new ApiGatewayManagementApi({
        endpoint: 'my-apigateway-websocket-url',
      });
  
      const params = {
        ConnectionId: connectionId,
        Data: Buffer.from('pong'),
      };
  
      await api.postToConnection(params).promise()
  </code></pre>