Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
FreeSwitch Promises
CoffeeScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
docs
src
test
.gitignore
Cakefile
README.md
UNLICENSE
package.json

README.md

NOTE: This module was a prototype for the esl module 1.0 version. Please use the esl module instead of this one.

Chainable client and server for FreeSwitch events socket

Install

npm install fsp

Overview

This module offers two FreeSwitch event socket connection types, client() and server().

Typically a client would be used to trigger calls asynchronously (for example in a click-to-dial application); this mode of operation is called inbound (to FreeSwitch) in the Event Socket FreeSwitch documentation.

A server will handle calls sent to it using the socket diaplan application (called outbound mode in the Event Socket Outbound FreeSwitch documentation). The server you create is available at a pre-defined port which the socket dialplan application will specify.

You will use one mode or the other depending on what your application needs to do.

Usage

var FS = require('fsp');

Client Example

The following code does the equivalent of fs_cli -x: it connects to the Event Socket, runs a single command, then disconnects.

var FS = require('fsp');

var fs_command = function (cmd) {
  var client = FS.client();

  // Manage the client using the chainable API.
  client
  .connect(8021, '127.0.0.1')
  .on('freeswitch_connect') // wait for indication that FreeSwitch is ready
  .then FS.api(cmd)       // execute the command
  .then FS.exit()         // send the `exit` command to FreeSwitch
  .then FS.disconnect();  // cleanly disconnects from FreeSwitch
};

// Example: execute `reloadxml`.
fs_command("reloadxml");

Note: Use handler.event_json 'HEARTBEAT' to start receiving event notifications.

CallServer Example

From the FreeSwitch dialplan, use

<action application="socket" data="127.0.0.1:7000 async full"/>

to hand the call over to an Event Socket server on the local host on port 7000.

Typically a server will send commands to FreeSwitch, wait for completion, send a new command, etc.

var FS = require('fsp');

// The call handler is called every time FreeSwitch sends us a new call.
var call_handler = function(pv) {
  pv
  .then( FS.command('play-file', 'voicemail/vm-hello') )
  .then( function (pv) {
    var foo = pv.body.variable_foo;
    if(foo) {
      return FS.command('play-file', 'digits/1')(pv);
    }
    return pv;
  })
  // Asynchronous call to a database, a website, etc.
  .then( function(pv) {
    return request('http://127.0.0.1/some/value');
  })
  .then( function(data) {
      if(data) {
        // Wait for the command to complete on FreeSwitch.
        FS.command('play-file',data)(pv);
      }
  });

};

var server = FS.server(call_handler);
server.listen(7000);

For some applications you might want to capture channel events instead of using the command() / callback pattern:

var FS = require('fsp');

var call_handler = function(pv) {
  var uri = req.body.variable_sip_req_uri;
  pv.on('CHANNEL_ANSWER', function(pv) {
      util.log('Call was answered');
  });
  pv.on('CHANNEL_HANGUP_COMPLETE', function(pv) {
      util.log('Call was disconnected');
  });
};

var server = FS.server(call_handler);
server.listen(7000);

Alternative

The present module should be more convenient if you've already coded for Node.js and are used to using promises or the EventEmitter patterns. If you are coming from the world of FreeSwitch and are used to the Event Socket Library API, you might want to try node-esl.

Something went wrong with that request. Please try again.