Skip to main content

composer.json

Open in GitHub
{
//
"require": {
"php": "^7.3|^8.0",
//
"beyondcode/laravel-websockets": "^1.12",
//
"pusher/pusher-php-server": "~3.0"
},
//
}

resources/js/bootstrap.js

Open in GitHub
import 'alpinejs';
 
import Echo from 'laravel-echo'
 
window.Pusher = require('pusher-js');
 
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
wsHost: window.location.hostname,
wsPort: 6001,
wssPort: 6001,
disableStats: true,
});

routes/channels.php

Open in GitHub
use Illuminate\Support\Facades\Broadcast;
 
//
Broadcast::channel('live.count.{channel}', function ($user, $channel) {
return (int) $user->id === (int) \App\Models\Channel\Channel::where('slug', $channel)->first()->owner_id;
});
//

app/Events/UserNotifications.php

Open in GitHub
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
 
class UserNotifications implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
 
public $user;
 
public function __construct($user)
{
$this->user = $user;
}
 
public function broadcastOn()
{
return new PrivateChannel('user.notifications.'. $this->user->id);
}
}

app/Http/Livewire/Frontend/Channel/Partial/ChannelAnalytics.php

Open in GitHub
use App\Models\Channel\Channel;
use Livewire\Component;
 
class ChannelAnalytics extends Component
{
public $channel_id;
public $count;
public $channel;
 
public function mount()
{
$this->getData();
}
 
public function getData()
{
$this->channel = Channel::find($this->channel_id);
$this->count = $this->channel->subscribers()->count();
}
 
public function getListeners()
{
return [
"echo-private:live.count.{$this->channel->slug},LiveCount" => 'getData'
];
}
 
public function render()
{
return view('livewire.frontend.channel.partial.channel-analytics');
}
}

resources/views/livewire/frontend/channel/partial/channel-analytics.blade.php

Open in GitHub
<div class="mt-6">
<div>
<p><i class="fas fa-spinner fa-pulse"></i> {{ __('Updating Live') }}</p>
</div>
<div class="h-96 w-auto flex flex-col justify-center items-center">
<h1 class="font-bold text-7xl">{{ ReadableNumber($count) }}</h1>
<p class="font-medium text-2xl">{{ \Illuminate\Support\Str::plural('subscriber', $count) }}</p>
</div>
</div>

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.