asterisk / node-ari-client

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

Event 'EndpointStateChange' not emitting #122

Closed VasylDmytruk closed 5 years ago

VasylDmytruk commented 5 years ago

I try to handle endpoint state change. I connect to asterisk via Zoiper and see in list of endpoints that stateof my resource changed from 'offline' to 'online'. And when I exit from Zoiper vice versa from 'online' to 'offline'. I can see this in list of endpoints:

var client = require('ari-client');
client.connect('http://localhost:8088', 'my-username', 'my-password')
    .then(function (ari) {
        ari.endpoints.list()
            .then(function (endpoints) {
                console.log('endpoints.length', endpoints.length);
                endpoints.forEach(endpoint => {
                    console.log(endpoint.resource + ' ' + endpoint.state);
                })
            })
            .catch(function (err) {
            });
    })
    .catch(function (err) {
        console.error('error', err);
    });

I want to see state change in event EndpointStateChange:

var client = require('ari-client');
client.connect('http://localhost:8088', 'my-username', 'my-password')
    .then(function (ari) {

        ari.on('EndpointStateChange', function (event, endpoint) {
            // doesn't work
            console.log('endpoint', endpoint);
        });

        ari.start('app');
    })
    .catch(function (err) {
        console.error('error', err);
    });

What I am doing wrong?

samuelg commented 5 years ago

It's been a while since I've worked with endpoints, but I believe you have to subscribe to endpoint events in order to receive them.

Something like:

ari.applications.subscribe({
  // your app name
  applicationName: 'app',
  // https://wiki.asterisk.org/wiki/display/AST/Asterisk+15+Applications+REST+API#Asterisk15ApplicationsRESTAPI-subscribe
  eventSource: `endpoint:${endpoint. technology}/${endpoint.resource}`
}).then(() => {})
   .catch((err) => {});
VasylDmytruk commented 5 years ago

Thank you very much! It works!!!

VasylDmytruk commented 5 years ago

Could you also tell how to handle theses events:

    ari.on('StasisStart', function (event, channel) {
        console.log('StasisStart', channel);
    });
    ari.on('StasisEnd', function (event, channel) {
        console.log('StasisEnd', channel);
    });

My code is:

    var client = require('ari-client');
    client.connect('http://localhost:8088', 'my-username', 'my-password')
        .then(function (ari) {

            ari.on('StasisStart', function (event, channel) {
                // doesn't work
                console.log('StasisStart', channel);
            });
            ari.on('StasisEnd', function (event, channel) {
                // doesn't work
                console.log('StasisEnd', channel);
            });

            ari.start('app');
        })
        .catch(function (err) {
            console.error('error', err);
        });