Aekansh Dixit Aekansh Dixit - 5 months ago 63
PHP Question

PHP Pusher Event Triggering not working

I am a newbie when it comes to WebSockets, and I recently came across Pusher. I wanted to integrate it in my website. I am trying to trigger an event via the Pusher.php file, and here is my code:

$pusherArray['success'] = true;
$pusher = new Pusher( $PUSHER_APP_KEY, $PUSHER_APP_SECRET, $PUSHER_APP_ID );
class MyLogger {
public function log( $msg ) {
print_r( $msg . "\n" );
}
}

$pusher->set_logger( new MyLogger() );
$pusher->trigger('8307851079', 'logout', $pusherArray);
$info = $pusher->get_channel_info("$clef_id");
$channel_occupied = $info->occupied;
var_dump($channel_occupied);


And then this is the result I get:

Pusher: curl_init( http://api.pusherapp.com:80/apps/217851/events?auth_key=MY_KEY&auth_signature=SIGNATURE&auth_timestamp=1466342695&auth_version=1.0&body_md5=41b74623d1e5f479b466093805ff36de )
Pusher: trigger POST: {"name":"logout","data":"{\"success\":true}","channels":8307851079}
Pusher: exec_curl response: Array
(
[body] => Expected channels parameter to be an array

[status] => 400
)

Pusher: curl_init( http://api.pusherapp.com:80/apps/217851/channels/8307851079?auth_key=MY_KEY&auth_signature=SIGNATURE&auth_timestamp=1466342696&auth_version=1.0 )
Pusher: exec_curl response: Array
(
[body] => 404 NOT FOUND

[status] => 404
)

NULL


The event is not getting triggered! Need some help!

Answer

I got it to work eventually. What had happened was that the view channel info method was causing the error. I did not have proper syntax.

And as pointed out Rob, the first argument of the trigger function required it to be an array. I changed that and got the script to work. Here's the brief code:

    $pusherArray['success'] = true; 
$pusher = new Pusher( $PUSHER_APP_KEY, $PUSHER_APP_SECRET, $PUSHER_APP_ID );
class MyLogger { public function log( $msg ) { print_r( $msg . "\n" ); } } 
$pusher->set_logger( new MyLogger() ); 
$pusher->trigger(['8307851079'], 'logout', $pusherArray);

I just simply removed the view channel info because I didn't really need that function. Got a 200 OK status code and my script was able to deliver the

"success":true

Message to the Pusher service.

Comments