Search code examples
phplaravelvonage

Web browser audio call using Nexmo PHP Laravel


I want to make a call from a web application(PHP/Laravel) to any mobile number from (purchased Nexmo Number). It's like a two-way communications call.

The scenario assumes in the web application(PHP/Laravel) page display the call driver options placed the call icon. Once customers click the call icon then call to driver number from (purchased Nexmo Number).

I have used this API to create a call.

    $ncco = [
      [
        'action' => 'talk',
        'voiceName' => 'Joey',
        'text' => 'This is a text-to-speech test message.'
      ]
    ];

    $call = new \Nexmo\Call\Call();
    $call->setTo('XXXXXXXXXXXX')
      ->setFrom('XXXXXXXXXXXX')
      ->setNcco($ncco);

    $response = $client->calls()->create($call);
    echo $response->getId();

The Nexmo Voice API here one-way communication only working fine for me. For example, text to speech call works for me, the above voice API code run the call automatically reached to destination number from (purchased Nexmo Number).

Is anybody has done this scenario? when you click on the phone icon it will call customers + you can talk with customer using a web portal?


Solution

  • There are two ways to go about this.

    Call Bridging

    You can bridge two numbers together by having the system call you, and if you answer then call someone else to bridge them in. This can all be done on the server side much like what you have above, the NCCO just changes slightly.

    $ncco = [
        [
            'action' => 'connect',
            'endpoint' => [
                [
                    'type' => 'phone',
                    'number' => DRIVER_NUMBER
                ]
            ]
        ]
    ];
    
    $call = new \Nexmo\Call\Call();
    $call->setTo(CUSTOMER_NUMBER)
      ->setFrom(VONAGE_NUMBER)
      ->setNcco($ncco);
    
    $response = $client->calls()->create($call);
    echo $response->getId();
    

    The only real problem with this is the user experience. The user probably expects the call work like a real phone call (click the button, hear ringing, hope the driver connects). You would need to add some additional NCCO options like streaming a ring tone, checking to see if the other person declines the call or never answers and responding appropriately, etc, but it can be done by pushing some NCCOs around and watching the voice events.

    In Browser/In App

    The other option is our Client SDK, which is available for front-end JavaScript, iOS, and Android. This can be used to place a call from a browser or app, and do functionally the same but from within a dedicated interface. A short tutorial can be found at https://developer.nexmo.com/client-sdk/tutorials/app-to-phone/introduction/javascript.