Asterisk AMI Client for NodeJS (ES2015)
Full functionality client for Asterisk's AMI. Support any data packages (action/event/response/custom responses) from AMI; With this client you can select you'r own case of programming interactions with Asterisk AMI.
If you like events & handlers - you can use it!
If you like promises - you can use it!
If you like co
& sync-style of code - you can use it!
Install
$ npm i asterisk-ami-client
NodeJS versions
support >=4.0.0
Usage
It is only some usage cases.
Example 1:
Listening all events on instance of client;
const AmiClient = ;let client = ; client ;
Example 2:
Receive Asterisk's AMI responses with promise-chunk.
const AmiClient = ;let client = reconnect: true; client ;
or with co
-library for sync-style of code
Example 3:
Receive Asterisk's AMI responses with co
.
const AmiClient = ;const co = ; let client = reconnect: true; ;
Example 4:
Listening event
and response
events on instance of client.
const AmiClient = ; let client = reconnect: true keepAlive: true; client ;
Example 5:
Emit events by names and emit of response by resp_${ActionID}
(if ActionID is set in action's data package).
const AmiClient = ; let client = reconnect: true keepAlive: true emitEventsByTypes: true emitResponsesById: true; client ;
More examples
For more examples, please, see ./examples/*
.
Docs & internal details
Events
connect
- emits when client was connected;event
- emits when was received a new event of Asterisk;data
- emits when was received a new chunk of data form the Asterisk's socket;response
- emits when was received a new response of Asterisk;disconnect
- emits when client was disconnected;reconnection
- emits when client tries reconnect to Asterisk;internalError
- emit when happens something very bad. Like a disconnection from Asterisk and etc;${eventName}
- emits when was received event with nameeventName
of Asterisk and parameteremitEventsByTypes
was set totrue
. See example 5;${resp_ActionID}
- emits when was received response withActionID
of Asterisk and parameteremitResponsesById
was set totrue
. See example 5.
Client's parameters
Default values:
reconnect: false maxAttemptsCount: 30 attemptsDelay: 1000 keepAlive: false keepAliveDelay: 1000 emitEventsByTypes: true eventTypeToLowerCase: false emitResponsesById: true dontDeleteSpecActionId: false addTime: false eventFilter: null // filter disabled
reconnect
- auto reconnection;maxAttemptsCount
- max count of attempts when client tries to reconnect to Asterisk;attemptsDelay
- delay (ms) between attempts of reconnection;keepAlive
- when istrue
, client sendAction: Ping
to Asterisk automatic every minute;keepAliveDelay
- delay (ms) between keep-alive actions, when parameterkeepAlive
was set totrue
;emitEventsByTypes
- when istrue
, client will emit events by names. See example 5;eventTypeToLowerCase
- when istrue
, client will emit events by names in lower case. Uses withemitEventsByTypes
;emitResponsesById
- when istrue
and data package of action has ActionID field, client will emit responses byresp_ActionID
. See example 5;dontDeleteSpecActionId
- when istrue
, client will not hide generated ActionID field in responses;addTime
- when istrue
, client will be add into events and responses field$time
with value equal to ms-timestamp;eventFilter
- object, array or Set with names of events, which will be ignored by client.
Methods
.connect(username, secret[, options])
- connect to Asterisk. See examples;.disconnect()
- disconnect from Asterisk;.action(message)
- send new action to Asterisk;.write(message)
- alias ofaction
method;.send(message)
- alias ofaction
method;.option(name[, value])
- get or set option of client;.options([newOptions])
- get or set all options of client.
Properties
Getters
lastEvent
- last event, which was receive from Asterisk;lastResponse
- last response which was receive from Asterisk;isConnected
- status of current connection to Asterisk;lastAction
- last action data which was transmitted to Asterisk;connection
- get current amiConnection.
Tests
Tests require Mocha.
mocha ./tests
or with npm
npm test
Test coverage with Istanbul
npm run coverage
License
Licensed under the MIT License