Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Spharian's avatar

broadcast() slows my requests so much

Hello,

I'm building a simple chat application and I discovered that dispatch(new ChatMessage)->toOthers() (even without the toOthers part) slows my request by approximatively 160ms. I'm using the database driver for queues but the same problem occurs in production where I'm using redis.

My app is quite simple and my NewChatMessage only contains:

<?php

namespace App\Events;

use App\Models\ChatMessage;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewChatMessage implements ShouldBroadcastNow, ShouldBeUnique
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct(ChatMessage $message)
    {
        $this->message = $message;
    }

    public function handle()
    {
    }

    public function broadcastOn()
    {
        return new PresenceChannel('chat.'.$this->message->room_id);
    }
}

I tried removing the data from the events (the $message instance) and nothing changed.

Broadcasting happens through pusher on development stage for dev and production paid plan for production.

Tried with and without caching events, no difference.

Any idea on this? Anyone encountered this problem?

Edit: BroadcastNow uses sync, whenever I remove the Now part, the request is faster, but my server configuration/project can't handle so much jobs in the queue (everything gets delayed easily).

King regards.

0 likes
2 replies
click's avatar

I don't have much experience with it so I can't give you information if 160ms of delay is normal. But I think the documentation is clear about why they use the queue:

You will also need to configure and run a queue worker. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected by events being broadcast.

https://laravel.com/docs/9.x/broadcasting#queue-configuration

Sinnbeck's avatar

Buy a cheap droplet server at digital ocean or hetzner for your web site. Then running a few queues with workers should not be a big problem. I use one for chat, with about 50-100 simultaneous users. The system allows sending to all in a group at once (like a room). The server handles it easily

Please or to participate in this conversation.