Laravel向推送器广播消息

时间:2020-08-03 07:07:05

标签: laravel broadcast pusher

我正在尝试通过laravel中的pusher广播消息,但出现以下错误:

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}    
});

正在发生的代码如下

Broadcast::channel('conversations.{id}', function ($user, $id) {
    return $user->inConversation($id);
});

我有另一个频道,它工作正常

Broadcast::channel('users.{id}', function ($user, $id){
    return (int) $user->id === (int) $id;
});

不确定为什么参数太少

***更新*** 我正在使用laravel livewire。

事件类如下:

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);
    }
}

laravel livewire功能如下:

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 = '';
    }

问候 丹尼

1 个答案:

答案 0 :(得分:0)

基本上,这里的解决方案是将BroadcastServiceProvider中的Broadcast :: routes设置为以下

Broadcast::routes(['middleware' => 'auth']);