5

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?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • I'm rather familiar with Asterisk and I've made some edits to your question to improve readability and clarity. Please check my edits to ensure they are correct. Feel free to make any changes that should be made, if I've made any mistakes. – Tim Post Mar 15 '12 at 07:54
  • @TimPost Thanks..! I always feel difficulty in writing HTML of coding here. Although i tried to read the help every time i post.
    And Tim can you please give me your Skype or email address i need to discuss few things with you that may help both of us.
    – Arham Ali Qureshi Mar 15 '12 at 19:01

2 Answers2

4

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

Nasir Iqbal
  • 909
  • 7
  • 24
0

I think you just have use php-agi.php class. it already have all you need. No any need write it again.

php-agi.php distributed with any asterisk and can be found in /var/lib/asterisk/agi-bin/

arheops
  • 15,544
  • 1
  • 21
  • 27
  • Actually, I just looked at the class you mentioned (was just going to edit an example in here for you) and my version of the class is ` @version 2.0`, I don't see an option for a persistent connection (though it does re-use the socket). Did I miss something? – Tim Post Mar 15 '12 at 08:09
  • I am not using AGI i am using AMI, but if you got any idea how AGI is better than AMI please let me know. In simple words, I searched a lot on google but can'd understand the difference. – Arham Ali Qureshi Mar 15 '12 at 18:32
  • 1
    AGI is control connection(i.e dialplan connect to program), while AMI is manager connection(external program connect to asterisk/listen events). sorry, i am missed. but general answer is same. there are some implementations of AMI class in PHP, no need create your own. search voip-info.org by AMI word. – arheops Mar 15 '12 at 20:57
  • @arheops actually i am creating my own web based application. I think i should use AMI, What you say?
    I have posted the above code from voip-info.org
    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.
    – Arham Ali Qureshi Mar 15 '12 at 21:47
  • you "should" use mix of ami, fastagi and dialplan+realtime architecture. You should not create "my own web application" if you not understand what gooing on. except variatn when application will be never more then 1-2 channels. if you still think you have, check code of OTHER people like this one http://code.google.com/p/asterisk-php-api/ – arheops Mar 16 '12 at 17:18