php - Laravel WebSocket with Pusher Error when making POST Request
I'm trying to integrate web socket into my laravel project. I did all steps from here
composer require pusher/pusher-php-server
/Users/alpha/Sites/jdoe/config/broadcasting.php
https://dashboard.pusher.com/apps/888
app_id = "888"
key = "***"
secret = "333"
cluster = "us2"
|
V
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=888
PUSHER_APP_KEY=***
PUSHER_APP_SECRET=333
PUSHER_APP_CLUSTER=us2
.env.example + .env
npm install --save laravel-echo pusher-js
npm run watch
I tested making a post to my /ws/log
{-code-2}
I kept getting
ErrorException: array_merge(): Expected parameter 2 to be an array, {-code-4} given in file /Users/alpha/Sites/jdoe/vendor/pusher/pusher-php-server/src/Pusher.php on line 518
I opened that file
{-code-3}
$params some how = {-code-4}
How do I continue to debug this further?
Edit
I removed my vendor/ and tried version 4.1 as suggested.
{-code-5}
I still face issue when making a POST to my route via POSTman.
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Answer
Solution:
So for this problem, in fact that was an issue in Laravel and it is solved now
using (laravel 8.29.0) or higher versions.
So pusher-http-php library v5.0.1 and Laravel v8.29.0 will work fine for you.
Another Solution: is to downgrade pusher to version 4.1 on composer.json and this version seems to work fine with older laravel versions (older than 8.29.0).
If you want to solve the problem with your current installation:
The problem is that the calls for trigger function, at least on laravel 8 is not sending the right parameters, so in PusherBroadcaster.php
and pusher-php-server/Pusher.php
there are 2 calls for trigger function which are:
$this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);
and
public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)
You will need to change the Pusher
only.
1- Change (Line 496).
$data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded ? $data : json_encode($data));
To:
$data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded && !is_array( $data) ? $data : json_encode($data));
2- Change (Line 499).
$data_encoded = $already_encoded ? $data : json_encode($data);
To:
$data_encoded = $already_encoded && !is_array( $data) ? $data : json_encode($data);
3- Change (Line 518)
$all_params = array_merge($post_params, $params);
To:
$all_params = array_merge($post_params, is_array($params) ? $params:[]);
4- Change (Line 542)
return $result;
To:
return $response;
and this will make it works for you fine.
Note: As I said it is resolved in v8.29.0 of Laravel. Link
Note: Or you can use composer require pusher/pusher-php-server ^4.1
Note that:
The main problem is that $param
is null
in this case and it didn't check if this value is array()
so when the code tries to merge array()
with null
.
Resources:
Source