Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I establish a persistent connection to an Asterisk manager with PHP?

I'm attempting to connect to an Asterisk manager interface, and I'm having a problem with the code blocking, as well as connection persistence. Below is what I have, followed by a description of what's going wrong:

/**
 * The parameters for connecting to the server
 */
 $params = array('server' => '192.168.1.100', 'port' => '5038');

/**
 * Instantiate Asterisk object and connect to server
 */
 $ast = new Net_AsteriskManager($params);

/**
 * Connect to server
 */
 try {
    $ast->connect();
 } catch (PEAR_Exception $e) {
    echo $e;
 }

 /**
  * Login to manager API
  */
  try {
    $ast->login('admin', 'abcdefghi');
 } catch(PEAR_Exception $e) {
    echo $e;
 }

The above code works, as far as connecting. I'm able to fetch data through it.

The issue is sending a query is taking quite long time, and when I observe the server in realtime mode (console) I see that the user admin is getting log out from server after output is sent.

In other words, 'admin' is getting logged out even though I have not explicitly logged out in the code. How can I make this connection persistent?

like image 392
Arham Ali Qureshi Avatar asked Oct 08 '22 09:10

Arham Ali Qureshi


1 Answers

Asterisk AMI does not automatically closes the connection however it is network layer who does it, when it detect no activity for long time (=timeout) it drops the connection. To make a connection persistence it is required to keep it busy (=keep alive), whenever connection is idle your application should send keep alive packets to destination server at specified interval (=TTL). We can use any type of command as keep alive packet like in asterisk you can use "Ping".

However if you are looking some existing ready to use solution then you can use some AMI Proxy for that. here are some known AMI proxies

like image 84
Nasir Iqbal Avatar answered Oct 12 '22 01:10

Nasir Iqbal