guard = $guard; $this->showCount = $showCount; $this->showAvatars = $showAvatars; $this->maxDisplay = $maxDisplay; $this->loadOnlineUsers(); } public function loadOnlineUsers() { try { $presenceService = app(PresenceService::class); $users = $presenceService->getOnlineUsers($this->guard); // Limit the display count $this->onlineUsers = $users->take($this->maxDisplay)->toArray(); $this->dispatch('users-online-updated', [ 'count' => $users->count(), 'users' => $this->onlineUsers ]); } catch (\Exception $e) { $this->onlineUsers = []; } } #[On('presence-updated')] public function refreshUsers() { $this->loadOnlineUsers(); } public function render() { return view('livewire.users-online'); } } /* Blade Template: resources/views/livewire/users-online.blade.php */ ?> {{-- resources/views/livewire/users-online.blade.php --}}
@if($showCount)
{{ count($onlineUsers) }} online
@endif
@forelse($onlineUsers as $user)
@if($showAvatars)
@if(isset($user['avatar']) && $user['avatar']) {{ $user['name'] }} @else
{{ substr($user['name'], 0, 1) }}
@endif
@endif

{{ $user['name'] }}

{{ isset($user['last_seen']) ? \Carbon\Carbon::parse($user['last_seen'])->diffForHumans() : 'Now' }}

@empty

No users online

@endforelse
*/ ?>