Skip to content

KAZOO Support Channels

This documentation is curated by 2600Hz as part of the KAZOO open source project. Join our community forums here for peer support. Only features in the docs.2600hz.com/supported space are included as part of our 2600Hz Support Services plan.

Ping (pong)#

This module receives a ping, and responds with a pong. It takes no arguments. This can be used to test a socket is up, or keep it alive by preventing it from becoming idle.

Example JavaScript#

// Put your Blackhole URL here
var socket = new WebSocket(URL)
var socketResponseListeners = {};

// From https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function getUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
  });
}

// Ping the Websocket, if we don't get a pong within a second then reopen the websocket
function checkOpen() {
  var receivedResponse = false;
  var data = {
    request_id: getUUID(),
    action: 'ping'
  };
  socketResponseListeners[data.request_id] = function(resp) {
    if (resp.data.response === 'pong') {
      receivedResponse = true;
    }
  }
  // Expect response within one second
  setTimeout(function() {
    if (!receivedResponse) {
      console.log('Socket is down!');
      // You'll probably want to handle reopening logic here!
    }
  }, 1000);
  socket.send(JSON.stringify(data));
}

socket.onmessage = function(message_event) {
  // Parse message data
  var data = JSON.parse(message_event.data);

  if (data.action === "reply") {
    // Send response to requestor, if there is one
    if (socketResponseListeners[data.request_id]) {
      socketResponseListeners[data.request_id](data);
      delete socketResponseListeners[data.request_id];
    }
  }
};