Infura websockets disconnect after an hour of in-activity so they suggest you ping / pong the connection at an interval less then an hour.

My problem is I'm new to server-side programming and don't know how to ping the websocket connection from my back-end (node.js).

When googling I found this framework of code (below). Is this what I'd put in my back-end to 'ping' the connection?

I'm unclear if the conditional socket.readyState !== 1 is universal/'the norm' for all websockets in JS. Same with socket.send("heartbeat"). If it is then I assume it would work for Infura as well.

If anyone already has a ping pattern set-up I'd love to hear your insight!

let socket = null;

function connect_socket() {
  socket = new WebSocket(ws_url);
  socket.on("close", connect_socket); // <- rise from your grave!

function heartbeat() {
  if (!socket) return;
  if (socket.readyState !== 1) return;
  setTimeout(heartbeat, 550000); 


Thanks for any help!

closed as off-topic by Ismael, Achala Dissanayake, shane, Briomkez, Nicolas Massart Oct 2 '18 at 11:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, Achala Dissanayake, shane, Briomkez, Nicolas Massart
If this question can be reworded to fit the rules in the help center, please edit the question.