php
50 lines · 9 steps
Broadcasting typing indicators in Laravel
A controller authorizes a request, then broadcasts a real-time typing event to everyone else on a conversation's presence channel.
Explained by
highlit
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Models\Conversation;
6use Illuminate\Http\JsonResponse;
7use Illuminate\Http\Request;
8use Illuminate\Support\Facades\Broadcast;
9
10class TypingIndicatorController extends Controller
11{
12 public function store(Request $request, Conversation $conversation): JsonResponse
13 {
14 $this->authorize('view', $conversation);
15
16 $validated = $request->validate([
17 'is_typing' => ['required', 'boolean'],
18 ]);
19
20 $user = $request->user();
21
22 Broadcast::on("presence-conversation.{$conversation->id}")
23 ->as('client-typing')
24 ->with([
25 'user' => [
26 'id' => $user->id,
27 'name' => $user->name,
28 'avatar' => $user->avatar_url,
29 ],
30 'is_typing' => $validated['is_typing'],
31 'at' => now()->toIso8601String(),
32 ])
33 ->toOthers()
34 ->sendNow();
35
36 return response()->json(status: 202);
37 }
38}
39
40Broadcast::channel('conversation.{conversation}', function ($user, Conversation $conversation) {
41 if (! $conversation->participants()->whereKey($user->id)->exists()) {
42 return false;
43 }
44
45 return [
46 'id' => $user->id,
47 'name' => $user->name,
48 'avatar' => $user->avatar_url,
49 ];
50});
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Presence channels let you broadcast ephemeral events like typing state without persisting them to a database.
- 2Returning attributes from a channel authorization callback both authorizes the user and shares their profile with other members.
- 3toOthers() and a 202 status keep the sender's own UI in sync while acknowledging a fire-and-forget event.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
php
<?php namespace App\Services;
Building a cached daily leaderboard in Laravel
caching
aggregation
eager-loading
Intermediate
9 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/broadcasting-typing-indicators-in-laravel-explained-php-afa3/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.