0

Using relay client api to send sms from Signalwire it gives an error message

"Call to a member function send() on null" in file vendor\signalwire\signalwire\src\Relay\Connection.php

$this->_ws->send($msg->toJson());

I am using these code below

use SignalWire\Relay\Client;

$client = new Client(['project' =>'project_id','token' => 'token_id']);

$params = [
    'context' => 'office',
    'from' => '+1++++',
    'to' => '+1++++',
    'body' => 'Welcome at SignalWire!'
];

Log::info('Sending SMS..');

$client->messaging->send($params)->done( function ($sendResult){

    if ($sendResult->isSuccessful()) {
        Log::info('SMS queued successfully!');
        echo 'success';
      
    } else {
        Log::warning('Error sending SMS!');
        echo 'error';
    }
});
STA
  • 30,729
  • 8
  • 45
  • 59
  • 1
    Seems ` $client->messaging` is null, check if your `$client` is having correct structure and have property called messaging. – Prafulla Kumar Sahu Nov 01 '22 at 05:13
  • I have followed these document of signalwire,as I checked I have used correct structure as per document. https://docs.signalwire.com/reference/relay-sdk-php/v2/#api-reference-relay-client-constructor https://docs.signalwire.com/reference/relay-sdk-php/v2/#api-reference-relay-messaging-methods-send This error is coming on websockets which used in signalwire package/api – prem chand Nov 01 '22 at 07:21
  • what is the output of $client? ```dd($client)``` – Ali Raza Nov 01 '22 at 12:51
  • ^ SignalWire\Relay\Client {#475 ▼ +uuid: "f7822654-6823-4543-bc37-b314365dcf4b" +host: "relay.signalwire.com" +project: "My project id" +token: "my token no" +sessionid: null +nodeid: null +connection: SignalWire\Relay\Connection {#494 ▶} +eventLoop: React\EventLoop\StreamSelectLoop {#489 ▶} +relayProtocol: null +signature: null +contexts: [] #_calling: null #_tasking: null #_messaging: null -_autoReconnect: true -_loopIsRunning: false -_idle: false -_executeQueue: [] -_subscriptions: [] -_reconnectTimer: null – prem chand Nov 01 '22 at 13:41

1 Answers1

0

Attach an event handler for sending messages.

$client->on('signalwire.ready', function($client) use ($params){
     $client->messaging->send($params)->done( function ($sendResult)       
       if ($sendResult->isSuccessful()) {
         echo "Message ID: " . $sendResult->getMessageId();
       } else {
         echo 'error';
       }
     });
})->on('signalwire.error', function(\Exception $error) {
    echo "Error";
});

signalwire.ready method indicates that the session has been established and all other methods can now be used.

So with the use of this event handler, you can resolve the bug "Call to a member function send() on null" in the file.

Binal Gajjar
  • 322
  • 1
  • 6