Realtime chat in Laravel without melting your server
Pusher, Redis queues and a few rules that keep Laravel chat and notifications fast on a busy platform.
Chat looks simple until a few hundred people use it at once. On a social music platform I work on, messages, notifications and social activity all run through Laravel broadcasting with Pusher and Redis queues. These are the rules that kept it fast.
1. Never broadcast inside the request
Laravel events that implement ShouldBroadcast are pushed onto your queue, not sent during the HTTP request. Use that. The user's "send" call should save the message and return; the broadcast happens a few milliseconds later on a worker. Give broadcasts their own queue so a slow email job can't hold up chat.
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public bool $afterCommit = true;
public function __construct(public Message $message) {}
public function broadcastOn(): array
{
return [new PrivateChannel('conversation.'.$this->message->conversation_id)];
}
public function broadcastAs(): string
{
return 'message.sent';
}
public function broadcastQueue(): string
{
return 'broadcasts';
}
}
Then run a worker that listens to that queue first:
php artisan queue:work redis --queue=broadcasts,default
2. Broadcast after the database commit
$afterCommit = true matters more than it looks. Without it, a worker can pick up the event before the transaction that created the message has committed, and the other user's app fetches a message that doesn't exist yet.
3. Send small payloads
By default Laravel serialises every public property of the event. A message model with its relations loaded can easily be several kilobytes, and Pusher has a size limit per event. Define exactly what the client needs:
public function broadcastWith(): array
{
return [
'id' => $this->message->id,
'body' => $this->message->body,
'sender_id' => $this->message->sender_id,
'created_at' => $this->message->created_at->toIso8601String(),
];
}
4. Authorise channels properly
Private channels are only private if the authorisation callback checks something real. Check membership, not just "is logged in":
// routes/channels.php
Broadcast::channel('conversation.{conversation}', function (User $user, Conversation $conversation) {
return $conversation->participants()->whereKey($user->id)->exists();
});
5. Don't echo the sender's own message
The sender already has the message in their UI. Broadcast with toOthers() so they don't get a duplicate:
broadcast(new MessageSent($message))->toOthers();
On the client, Laravel Echo listens for the custom event name. Note the leading dot when you use broadcastAs():
window.Echo.private(`conversation.${conversationId}`)
.listen('.message.sent', (message) => {
messages.value.push(message)
})
The result
None of this is exotic, and that's the point. Queue the broadcasts, wait for the commit, keep payloads small and check channel access properly, and Laravel's built-in broadcasting holds up well under real traffic.