PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
217: Presence Channels and Online User Tracking
We've already covered how to send a private message to a specific user, but that's a one-way street. In a real-world app—like a collaborative document editor or a support dashboard—you often need to know who is currently looking at the same thing as you. That's where Presence Channels come in.
I'm going to show you how to build a "Who's Viewing This Ticket" indicator for a support system. The goal is simple: when an agent or a customer opens a support ticket, everyone else viewing that same ticket sees their name pop up in a list in real-time.
Authorizing the Presence Channel
Unlike private channels, which just return a boolean (true or false) to allow access, presence channels need to return an array of data. This data is what gets broadcast to everyone else when a user joins. If you return false, they're blocked; if you return an array, they're "present."
// routes/channels.php
Broadcast::channel('ticket.{ticketId}', function ($user, $ticketId) {
// Check if the user is actually assigned to this ticket or is the owner
if ($user->canViewTicket($ticketId)) {
return [
'id' => $user->id,
'name' => $user->name,
'avatar' => $user->avatar_url
];
}
});
I'm returning the avatar_url here because it's much nicer to show a small circle with a face than just a text string. Keep this array lean, though. Don't dump the whole User model in here; remember, this data is sent to every single client on the channel.
Wiring up the Frontend Listeners
Now we need the JavaScript side to handle three specific events: here (who is already there), joining (someone just arrived), and leaving (someone closed the tab). I usually set up a simple array in my JS state to keep track of these users.
Echo.join(`ticket.${ticketId}`)
.here((users) => {
// This fires immediately upon joining
this.onlineUsers = users;
this.renderUserList();
})
.joining((user) => {
this.onlineUsers.push(user);
this.renderUserList();
})
.leaving((user) => {
this.onlineUsers = this.onlineUsers.filter(u => u.id !== user.id);
this.renderUserList();
});
The "Why is my list empty?" Moment
I'll be honest: the first time I implemented this in a production project, I spent an hour scratching my head because the here event was never firing. I checked my network tab, and the connection was successful (101 Switching Protocols), but my list stayed empty.
The mistake? I had used Echo.private() instead of Echo.join(). It's a tiny semantic difference in the code, but a huge difference in the backend. A private channel is for security; a presence channel is for membership. Because I used .private(), the server didn't treat the connection as a "presence" event, and it never sent back the list of active users.
If you see your connection working but your here callback isn't triggering, double-check that you're using .join() on the frontend and that your channels.php is returning an array, not just true.
Handling the "Ghost" User
One thing to keep in mind is that presence channels rely on the WebSocket connection staying open. If a user's internet blips or their browser crashes, there's a slight delay before the server realizes they're gone and triggers the leaving event. I've found it's best to avoid using this for mission-critical "locking" (like preventing two people from editing the same field at the exact same millisecond) and instead use it as a visual cue. For hard locks, you'll still want a database-backed lock or a Redis key with a TTL.
📋 Practical Task
Exercise: Implementing a "Live Team" Status Bar
You are building a team dashboard. Your task is to implement a presence channel called team.{teamId}.
- Backend: Modify the channel authorization logic to ensure that only users who belong to the specified
teamIdcan join. Instead of just returning the user's name, return an array containing theirnameand theirrole(e.g., 'Admin', 'Editor', 'Viewer'). - Frontend: Write the Echo
.join()logic to maintain a list of online team members. When a user joins or leaves, the list should update. - Challenge: Add a condition in the
joininglistener that triggers a browser notification or a console log only if the user joining has the 'Admin' role.
There are no comments for now.