asterisk / node-ari-client

Node.js client for ARI. This library is best effort with limited support.
Other
250 stars 98 forks source link

The correct way to make a call #74

Closed TomDmitriev closed 7 years ago

TomDmitriev commented 7 years ago

Surprisingly, I haven't found an example of this. Here is how I am doing it

client.start('hello-world');

client.channels.create({app: 'hello-world', endpoint: 'Local/79991111111@sip-dialout'}).then(localChannel => {
    console.log('STAGE 1: ', localChannel.state);

    localChannel.dial().then(() => {
        console.log('STAGE 2: ', localChannel.state);
    }).error(function(err) {
        console.log('ERROR 2:', err);
    });
}).error(function(err) {
    console.log('ERROR 1: ', arguments);
});

Which usually works, i.e. I get a phone call on +79991111111 but sometimes instead I get the following error

STAGE 1: Down ERROR 2: Error: {"message":"Channel not found"} at SwaggerRequest.swaggerError [as errorCallback] (/usr/lib/node_modules/ari-client/lib/resources.js:944:19) at Object.error (/usr/lib/node_modules/ari-client/node_modules/swagger-client/lib/swagger.js:1077:24) at EventEmitter.error (/usr/lib/node_modules/ari-client/node_modules/swagger-client/lib/swagger.js:1296:19) at emitOne (events.js:96:13) at EventEmitter.emit (events.js:188:7) at emit (/usr/lib/node_modules/ari-client/node_modules/shred/lib/shred/request.js:454:21) at /usr/lib/node_modules/ari-client/node_modules/shred/lib/shred/request.js:473:9 at setBodyAndFinish (/usr/lib/node_modules/ari-client/node_modules/shred/lib/shred/response.js:103:7) at IncomingMessage. (/usr/lib/node_modules/ari-client/node_modules/shred/lib/shred/response.js:120:7) at emitNone (events.js:91:20) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)

This is the context from extensions.conf

[sip-dialout]
exten => _X.,1,Dial(SIP/zebra/+${EXTEN})
exten => _X.,n,Hangup()

What am doing wrong?

TomDmitriev commented 7 years ago

Here is what I've found in asterisk logs

[Dec 9 11:34:08] WARNING[7335] res_stasis.c: Could not find app 'hello-world' [Dec 9 11:34:08] ERROR[7336] res_stasis.c: Stasis app 'hello-world' not registered

Apparently, client.channels.create is executed before the app actually starts. But how can I ensure that it has started apart from using setTimeout or something like that?

TomDmitriev commented 7 years ago

client.start returns a Promise. Solved.