Client and Server for FreeSwitch Event System
The following code does the equivalent of
fs_cli -x: it connects to the Event Socket, runs a single command, then disconnects.
FS = require'esl';varvar client = FSclientthisapicmdthen// res contains the headers and body of FreeSwitch's response.resbodyshouldmatch/\+OK/;thenthisexit;thenclientend;;clientconnect8021'127.0.0.1';;fs_command"reloadxml";
The API methods return promises.
The original example as CoffeeScript:
FS = require 'esl'=client = FSclient ->@api cmdthen -> @exitthen -> clientendclientconnect 8021'127.0.0.1'fs_command 'reloadxml'
From the FreeSwitch XML dialplan, you can connect to an Event Socket server using for example:
Here is a simplistic event server:
varthiscommand'play-file' 'voicemail/vm-hello'thenvar foo = resbodyvariable_foo;hangup // hang-up the callexit // tell FreeSwitch we're disconnecting;require'esl'servercall_handlerlisten7000;
During development it is often useful to be able to see what messages are sent to FreeSwitch or received from FreeSwitch. This module uses the debug module for tracing; simply call you application with
to see traces.
The name available are
npm install esl
The test suite in
test/0001.coffee.md provides many examples.
The methods available inside the call-handler are those of the response object:
This module is modelled after Node.js' own httpServer and client, and uses an event-driven interface wrapper inside a promise-based API.
It offers two Event Socket handlers,
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 is available at a pre-defined port which the
socket dialplan application will specify.
call.event_json 'HEARTBEAT' to start receiving event notifications.
For some applications you might want to capture channel events instead of using the
command() / callback pattern:
var esl = require'esl'util = require'util';var# for debuggingthistracetrue;# These are called asynchronouslythisonce'CHANNEL_ANSWER'thenutil.log'Call was answered';;thisonce'CHANNEL_HANGUP'thenutil.log'Call hangup';;thisonce'CHANNEL_HANGUP_COMPLETE'thenutil.log'Call was disconnected';;# However note that `on` cannot use a Promise since it only would# get resolved oncethison'SOME_MESSAGE'util.log'Got Some Message';;;var server = eslservercall_handlerserverlisten3232;
The present module should be more convenient if you've already coded for Node.js and are used to promises and events. 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.