For the past month I’ve been experimenting with Node.js and I have to say I am at a loss of words on how awesome Node is.
So in an attempt to get acquainted with Node I’ve started porting my Shift8 library in Node. The whole asynchronous model of Node is perfect for handling the events emitted by asterisk to be further emitted by node.
The library is still work in progress but it should be usable already and I decided to make it public so that I could get feedback quickly. I’ve recently ported the whole function list from the PHP version of Shift8. Please help me test it
Unfortunately there is a bug in Asterisk Manager Interface that would cause the node.js parser to crash. I’ve attached a patch with the source and I have also opened an issue with the asterisk team, hopefully soon to be mainstream.
Get it at Github @ https://github.com/twmobius/node-shift8
Comments are more than welcome
Are you trying do do something like APE so that the events gets passed to the client real time?
Hi Henry,
Well I guess that this is one application you could build with it.
i am newbie in node.js, could you make an example like Simple Channel Monitoring via Shift8. how can i see events on client’s browsers ?
Hi Kleen,
Check out https://github.com/twmobius/node-shift8/blob/master/tests.js
Shift8 will emit an event so on the “t.on(‘event’, function( event ) {}” you can capture the event emitted and process it accordingly.
Some of the events that you are probably interested in, are Newchannel and Hangup so you could have something like:
var activeCalls = [];
t.on(‘event’, function(event) {
if( event.event == ‘NewChannel’ ) {
activeCalls[event.uniqueid] = event;
}
else if( event.event == ‘Hangup’ ) {
delete activeCalls[event.uniqueid];
}
});
Something along these lines. (I haven’t tried to run it but it should work and you will end up with an array of all of your active calls – Well I guess you should get a listing of all the calls currently open, but this is a separate command)
Then from that point on you could pass the event/information down by means of Client->Node.js server communication path (Comet/Long Polling/socket.io etc)
Hope I’ve helped a bit
thank you for responding mobius, i can run tests.js as node test.js command in cli and i got events, but i don’t understand what can i call this js from browser ?
Hi Kleen,
I guess that you haven’t used node.js that much. Node.js is a framework to run javascript on a server (instead of a client). You could use node.js to build an http server to push events down to the client. So in sense you need node.js on one side (server), and some other javascript on the other (client browser).
Check out socket.io. It is by far the best way to have a node server speak to a client.
hi, I am trying to use node.js and shift8, and have got it working, up to a point. I seem to be getting duplicate messages on the console: see below:
{ event: ‘WaitEventComplete’ }
{ event: ‘WaitEventComplete’ }
{ event: ‘Newexten’,
privilege: ‘dialplan,all’,
timestamp: ’1319449122.626232′,
channel: ‘SIP/purple-000000e2′,
context: ‘conferenceroom’,
extension: ‘h’,
priority: ’2′,
application: ‘NoOp’,
appdata: ”,
uniqueid: ’1319449117.274′ }
{ event: ‘Newexten’,
privilege: ‘dialplan,all’,
timestamp: ’1319449122.626232′,
channel: ‘SIP/purple-000000e2′,
context: ‘conferenceroom’,
extension: ‘h’,
priority: ’2′,
application: ‘NoOp’,
appdata: ”,
uniqueid: ’1319449117.274′ }
{ event: ‘PeerStatus’,
privilege: ‘system,all’,
timestamp: ’1319449245.446487′,
channeltype: ‘SIP’,
peer: ‘SIP/3cx1′,
peerstatus: ‘Registered’,
address: ’192.168.99.100:61649′ }
{ event: ‘PeerStatus’,
privilege: ‘system,all’,
timestamp: ’1319449245.446487′,
channeltype: ‘SIP’,
peer: ‘SIP/3cx1′,
peerstatus: ‘Registered’,
address: ’192.168.99.100:61649′ }
is this something I’ve done ?
asterisk 10
Hi Julian,
I had a console.log() forgotten. Probably this is the case. Get the latest version from github and try again please.
hmm. Think that I had two sessions. However, when running node test.js, if you press ctrl-c it exists without logging the manager off. How can I disconnect the session ?
heh. Just seen your post. will try again.
You should probably do something like:
process.on(‘SIGINT’, function () {
t.logoff();
});
Thanks mobius for Shift8 – It looks as if it will provide some good glue
I am a newbie at node.js and javascript, so it is a little daunting – would you be able to give me an idea on how to call (for example) the getQueueStatus function from test.js ? What is the callback parameter for ? Thanks!
Julian,
You could check the source code. It is fully documented. In specific see this:
https://github.com/twmobius/node-shift8/blob/master/node-shift8.js#L332
I will see if I can create an API reference from the code (as in phpdoc)
I have read the code – the problem is that whilst I understand the functions, I don’t know how to call them from my own .js – what is callback, for example ? How do I capture the output from getQueueStatus ?
Thanks
Hi Julian,
The callback is a function of type function(error, result); So you should do something like
t.getQueueStatus(‘queue’, null, function(error, result) {
if( error ) {
console.log(“Unable to get result”);
return;
}
console.log(results);
});