Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_merge(): Expected parameter 2 to be an array #288

Closed
carlomigueldy opened this issue Feb 27, 2021 · 9 comments
Closed

array_merge(): Expected parameter 2 to be an array #288

carlomigueldy opened this issue Feb 27, 2021 · 9 comments

Comments

@carlomigueldy
Copy link

image

Whenever I try to dispatch an event I get this error

<warning>PHP Warning:  array_merge(): Expected parameter 2 to be an array, null given in /home/carlomigueldy/playground/laravel-chat-app/api/vendor/pusher/pusher-php-server/src/Pusher.php on line 518</warning>

Can anyone help me out?

This is my MessageSent() class code

<?php

namespace App\Events;

use App\Models\ChatMessage;
use App\Models\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    public $message;

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

    public function broadcastAs () {
        return 'message-sent';
    }

    // public function broadcastQueue () {
    //     return 'broadcastable';
    // }

    public function broadcastWith () {
        return [
            'text' => 'Awesome'
        ];
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('chat');
    }
}
@carlomigueldy carlomigueldy changed the title pusher/pusher-php-server array_merge(): Expected parameter 2 to be an array Feb 27, 2021
@lisandrop05
Copy link

I have the same problem seems to me that the code in the function that calls for trigger function, at least on laravel 8 is not sending the right parameters:

this is the line 112: in ...Illuminate/Broadcasting..../PusherBroadcaster.php

$response = $this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);

and this is the function in pusher-php-server/Pusher.php line 473:

public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)

I changed to:

public function trigger($channels, $event, $params = array(), $already_encoded = false, $data=null)

and the problem was solved for me at least for now, I'm very new on this.

additionally the PusherBroadcaster.php it's expecting a response to be an array and an object is sent back so I change the line # 542 in the Pusher.php as well from :

return $result;

to

return $response;

hope it helps at least for now until the problem is fixed

@carlomigueldy
Copy link
Author

I have the same problem seems to me that the code in the function that calls for trigger function, at least on laravel 8 is not sending the right parameters:

this is the line 112: in ...Illuminate/Broadcasting..../PusherBroadcaster.php

$response = $this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);

and this is the function in pusher-php-server/Pusher.php line 473:

public function trigger($channels, $event, $data, $params = array(), $already_encoded = false)

I changed to:

public function trigger($channels, $event, $params = array(), $already_encoded = false, $data=null)

and the problem was solved for me at least for now, I'm very new on this.

additionally the PusherBroadcaster.php it's expecting a response to be an array and an object is sent back so I change the line # 542 in the Pusher.php as well from :

return $result;

to

return $response;

hope it helps at least for now until the problem is fixed

Thank you for sharing your work-around!!

@carlomigueldy
Copy link
Author

It works now but my issue now that it is returning "true" instead of the array from broadcastWith()

image

@lisandrop05
Copy link

lisandrop05 commented Feb 28, 2021

It works now but my issue now that it is returning "true" instead of the array from broadcastWith()

image

I had same problem, after my workaround, I solved it by restoring the pusher to the original, and creating a new workaround:

there is three problems for me when calling from laravel 8 when calling trigger function of Pusher.php

Here is the line 112: in ...Illuminate/Broadcasting..../PusherBroadcaster.php
$response = $this->pusher->trigger(
$this->formatChannels($channels), $event, $payload, $socket, true
);
the problems are
1- $socket is set null instead of array,
2- laravel sets $already_encoded variable true, but sending data as array not json I don't understand why, the parameter is set as array but anyway send true to the function.
3- the function return just body instead of complete response

I'm avoiding to change the laravel PusherBroadcaster, so I changed the Pusher.php in order to correct that problems:

My workaround is like this:

1-
Line 496 of Pusher.php
changed from:
$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));

Line 499 of Pusher.php
changed from:
$data_encoded = $already_encoded ? $data : json_encode($data);
To:
$data_encoded = $already_encoded && !is_array( $data) ? $data : json_encode($data);

2-Line 518 of Pusher.php
changed from:
$all_params = array_merge($post_params, $params);
To:
$all_params = array_merge($post_params, is_array($params) ? $params:[]);

3- Line 542 of Pusher.php
changed from:
return $result;
To:
return $response;

using that it works for me, and Im getting messages on the browser:

Screen Shot 2021-02-28 at 12 22 08 PM

Screen Shot 2021-02-28 at 12 22 59 PM

@Koozza
Copy link
Contributor

Koozza commented Feb 28, 2021

Guys, Are you using laravel 8.29.0? I've reported this a while back: laravel/framework#36339 and this was fixed in 8.29.0: laravel/framework#36344

Please note that if you're using laravel-websockets you need to use at least 1.10 (beyondcode/laravel-websockets#698)

@benw-pusher
Copy link

Thanks @Koozza.

This should be resolved in v8.29.0 of Laravel. I'll close this issue now.

@LuisMejiaPoot
Copy link

I fixed it with this
#278 (comment)

@acidjazz
Copy link

Why is this still closed? can we close this when it works in Laravel again?

@guptanishantmca
Copy link

If you got this error it means, your pusher package is not compatible with the current Laravel version. To avoid this error just install the right package then it will work fine. I got the same error and resolved by installing the right compatible package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants