Search code examples
reactjslaravelratchetasteriskamireactphp

multiple asterisk ami connections with pami and react-php


I'm using PAMI with react-php and ratchet to connect with asterisk ami, I have 3 freepbx servers and want to connect with all of them in one dashboard and receive all the events from all the freepbx servers.

Here is the code that I wrote in php/laravel to connect with Asterisk ami. The question is, Can I use PAMI and react-php to connect to multiple freepbx servers?

Artisan::command('asterisk:go', function () {
$loop = React\\EventLoop\\Factory::create();
$websockethandler = new \\App\\WebsocketHandler();
$app = new Ratchet\\App(
'localhost',
6001,
'0.0.0.0',
$loop
);
$app->route(
'/websocket',
$websockethandler,
['\*'\]
);
$user               = AsteriskServer::where('user_id', auth()->user()->id)->get();
foreach($user as $key => $us){
try {

            $ami = new \React\Stream\DuplexResourceStream(
                stream_socket_client("tcp://$us->pbx:5038"),
                $loop
            );
    } catch (\Exception $e) {
        die($e->getMessage());
    }
    $asteriskmanager[$key] = new App\AsteriskManager($ami);
    $asteriskmanager[$key]->login(
        $us->ami_username,
        $us->ami_password
    );
    $callcenter = new Callcenter($websockethandler, $asteriskmanager[$key]);
    $websockethandler->on('websocket.hello', [$callcenter, 'websocketHello']);
    $websockethandler->on('websocket.avail', [$callcenter, 'websocketSetAgentAvail']);
    $websockethandler->on('websocket.pause', [$callcenter, 'websocketSetAgentPause']);
    
    $asteriskmanager[$key]->on('agent.loggedin', [$callcenter, 'agentLoggedIn']);
    $asteriskmanager[$key]->on('agent.events', [$callcenter, 'agentEvents']);
    $asteriskmanager[$key]->on('agent.paused', [$callcenter, 'agentPaused']);
    $asteriskmanager[$key]->on('agent.avail', [$callcenter, 'agentAvail']);

}

    $app->run();

})-\>purpose('Display an inspiring quote');

Solution

  • Yes you can, in fact you can take a look at https://github.com/clue/reactphp-ami which is a streaming, event-driven access to the Asterisk Manager Interface (AMI), built on top of ReactPHP.