Listening for broadcast events multi-tenancy

Hi guys, I'm new to using broadcasting in Laravel and I'm testing Laravel Reverb. Right now, I have it working so that when a user registers an appointment, if another logged-in user is on the same team, their appointment panel is updated with the reservation. So far, so good. Each user has X associated stores. I actually need it so that if user A, who is associated with stores 1 and 2, registers an event, it updates user B who is also associated with stores 1 and 2. Currently, it only works if both users are in the same store, but if they are in different stores, it doesn't update. What am I doing wrong? This is my code: //chanels.php:
Broadcast::channel('stores.{storeId}', function (User $user, int $storeId) {
$userStores = $user->stores()->pluck('id')->toArray();

if (in_array($storeId, $userStores)) {
return ['id' => $user->id, 'name' => $user->name];
}
});
Broadcast::channel('stores.{storeId}', function (User $user, int $storeId) {
$userStores = $user->stores()->pluck('id')->toArray();

if (in_array($storeId, $userStores)) {
return ['id' => $user->id, 'name' => $user->name];
}
});
//RefreshAppointmentsEvent.php:
class RefreshAppointmentsEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public Store $store)
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('stores.'.$this->store->id),
];
}
}
class RefreshAppointmentsEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public Store $store)
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('stores.'.$this->store->id),
];
}
}
//script for loading events:
<script>
window.addEventListener('DOMContentLoaded', function () {
window.Echo.channel('stores.{{ Filament\Facades\Filament::getTenant()->id }}')
.listen('RefreshAppointmentsEvent', (event) => {
console.log('test')
Livewire.dispatch('refreshData');
});
});
</script>
<script>
window.addEventListener('DOMContentLoaded', function () {
window.Echo.channel('stores.{{ Filament\Facades\Filament::getTenant()->id }}')
.listen('RefreshAppointmentsEvent', (event) => {
console.log('test')
Livewire.dispatch('refreshData');
});
});
</script>
1 Reply
Daniel Reales
Daniel Reales2mo ago
This is my Listener in the component Livewire:
public function getListeners(): array
{
return [
'echo-private:'.'stores.'.Filament::getTenant()->id.',RefreshAppointmentsEvent' => 'refreshData',
];
}
public function getListeners(): array
{
return [
'echo-private:'.'stores.'.Filament::getTenant()->id.',RefreshAppointmentsEvent' => 'refreshData',
];
}