I am attempting to broadcast a message via pusher in laravel but I am getting the following error:
Symfony\Component\Debug\Exception\FatalThrowableError · Too few arguments to function
App\Providers\BroadcastServiceProvider::{closure}(), 1 passed in
laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php on line 77 and exactly 2
expected routes/channels.php:33App\Providers\BroadcastServiceProvider::{closure}
});
The code that it's occuring on is the following
Broadcast::channel('conversations.{id}', function ($user, $id) {
return $user->inConversation($id);
});
I have another channel and it works fine
Broadcast::channel('users.{id}', function ($user, $id){
return (int) $user->id === (int) $id;
});
Not sure why there are too few arguments
*** UPDATE *** I'm using laravel livewire.
The event class is as follows:
class MessageAdded implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @param Message $message
*/
public function __construct(Message $message)
{
$this->message = $message;
}
public function broadcastWith()
{
return [
'message' => [
'id' => $this->message->id
]
];
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('conversations.' . $this->message->conversation->id);
}
}
The laravel livewire function is as follows:
public function reply()
{
$this->validate([
'body' => 'required'
]);
$message = $this->conversation->messages()->create([
'user_id' => auth()->id(),
'body' => $this->body
]);
$this->conversation->update([
'last_message_at' => now()
]);
foreach ($this->conversation->others as $user) {
$user->conversations()->updateExistingPivot($this->conversation, [
'read_at' => null
]);
}
broadcast(new MessageAdded($message))->toOthers();
$this->emit('message.created', $message->id);
$this->body = '';
}
Regards Danny
Basically, the solution here was to set the Broadcast::routes in the BroadcastServiceProvider to the following
Broadcast::routes(['middleware' => 'auth']);