Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Pusher array_merge: Expected parameter 2 to be an array, null given

i'm following a tutorial from pusher to display notification on the website. Everything has been in line with the tutorial, however this particular error showed up when i try to access the notification on localhost:8000/test i have no clue on how to fix it.

the error message

expected result : notification send message

output : array_merge() error

related tutorial : https://pusher.com/tutorials/web-notifications-laravel-pusher-channels

related file : C:\xampp\htdocs\inventory-prototype\vendor\pusher\pusher-php-server\src\Pusher.php:518

here's my Events/ItemAdd :

class ItemAdd implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;
    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
        $this->message = '{ $user } added an item';
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return ['item-add'];
    }
}

here's my web.php:

Route::get('test', function () {
    dd(event(new App\Events\ItemAdd('Someone')));
    return "Event has been sent!";
});

vendor/pusher/src/Pusher.php -> Trigger

    /**
     * Trigger an event by providing event name and payload.
     * Optionally provide a socket ID to exclude a client (most likely the sender).
     *
     * @param array|string $channels        A channel name or an array of channel names to publish the event on.
     * @param string       $event
     * @param mixed        $data            Event data
     * @param array        $params          [optional]
     * @param bool         $already_encoded [optional]
     *
     * @throws PusherException   Throws PusherException if $channels is an array of size 101 or above or $socket_id is invalid
     * @throws ApiErrorException Throws ApiErrorException if the Channels HTTP API responds with an error
     *
     * @return object
     */
    public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)
    {
        if (is_string($channels) === true) {
            $channels = array($channels);
        }

        $this->validate_channels($channels);
        if (isset($params['socket_id'])) {
            $this->validate_socket_id($params['socket_id']);
        }

        $has_encrypted_channel = false;
        foreach ($channels as $chan) {
            if (PusherCrypto::is_encrypted_channel($chan)) {
                $has_encrypted_channel = true;
            }
        }

        if ($has_encrypted_channel) {
            if (count($channels) > 1) {
                // For rationale, see limitations of end-to-end encryption in the README
                throw new PusherException('You cannot trigger to multiple channels when using encrypted channels');
            } else {
                $data_encoded = $this->crypto->encrypt_payload($channels[0], $already_encoded ? $data : json_encode($data));
            }
        } else {
            $data_encoded = $already_encoded ? $data : json_encode($data);
        }

        $query_params = array();

        $path = $this->settings['base_path'].'/events';

        // json_encode might return false on failure
        if (!$data_encoded) {
            $this->log('Failed to perform json_encode on the the provided data: {error}', array(
                'error' => print_r($data, true),
            ), LogLevel::ERROR);
        }

        $post_params = array();
        $post_params['name'] = $event;
        $post_params['data'] = $data_encoded;
        $post_params['channels'] = array_values($channels);

        $all_params = array_merge($post_params, $params);

        $post_value = json_encode($all_params);

        $query_params['body_md5'] = md5($post_value);

        $ch = $this->create_curl($this->channels_url_prefix(), $path, 'POST', $query_params);

        $this->log('trigger POST: {post_value}', compact('post_value'));

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value);

        $response = $this->exec_curl($ch);

        if ($response['status'] !== 200) {
            throw new ApiErrorException($response['body'], $response['status']);
        }

        $result = json_decode($response['body']);

        if (property_exists($result, 'channels')) {
            $result->channels = get_object_vars($result->channels);
        }

        return $result;
    }

any help will be appreciated

like image 898
ckaryuusai Avatar asked Mar 02 '21 05:03

ckaryuusai


2 Answers

i say whatever, i just downgraded to pusher 4.1, on composer.json look for pusher and change the version to 4.1 in case anybody on earth other than me get the same error.

like image 57
ckaryuusai Avatar answered Nov 14 '22 23:11

ckaryuusai


This error was resolved in the pusher-http-php library v5.0.1 and Laravel v8.29.0. https://github.com/pusher/pusher-http-php/issues/288

like image 32
doydoy Avatar answered Nov 14 '22 23:11

doydoy