Refresh a table when notification received

Is there any way to refresh a table when a broadcast notification is received?
Solution:
So I realised I can listen for a broadcast notification on the resource list class and then do something to the table to refresh it. My solution was to just set the tableSearch to itself which seems to work but feels a bit hacky ``` public function getListeners(): array { $user_id = auth()->user()->id;...
Jump to solution
2 Replies
Solution
Matt Jenkins
Matt Jenkins15mo ago
So I realised I can listen for a broadcast notification on the resource list class and then do something to the table to refresh it. My solution was to just set the tableSearch to itself which seems to work but feels a bit hacky
public function getListeners(): array
{
$user_id = auth()->user()->id;

return [
"echo-private:import.user.{$user_id},ImportComplete" => 'refreshTable'
];
}

public function refreshTable()
{
$this->tableSearch = $this->tableSearch;
}
public function getListeners(): array
{
$user_id = auth()->user()->id;

return [
"echo-private:import.user.{$user_id},ImportComplete" => 'refreshTable'
];
}

public function refreshTable()
{
$this->tableSearch = $this->tableSearch;
}
Brent Kelly
Brent Kelly7mo ago
Nice solution @Matt Jenkins. If running on a Page you can call ->refresh() on the table to refresh it - which is a bit cleaner than having to hack the tableSearch. So this should work:
public function refreshTable()
{
$this->table->refresh();
}
public function refreshTable()
{
$this->table->refresh();
}
... although I do notice this won't work for a table in a widget so 🤷. In that case assigning tableSearch as you have works still so possibly better to stick with that. For anyone else reading this, you can also use the #[On] syntax if you don't need to inject variables into the channel name:
#[On('echo-private:fooChannel,BarEvent')]
public function refreshTable()
{
$this->tableSearch = $this->tableSearch;
}
#[On('echo-private:fooChannel,BarEvent')]
public function refreshTable()
{
$this->tableSearch = $this->tableSearch;
}
Finally - hopefully this point will help someone else. I have been using Model Broadcasting to broadcast, but couldn't for the life of me get Livewire (or even Echo directly) to respond to the broadcast event: 1. The event was broadcasting fine (appearing in Pusher debug console) 2. Filament/Livewire was connecting to the channel fine (appearing in the Push debug console) 3. Devtools was showing the websocket receiving the broadcast message fine ... however the event just would not fire. 🤔 I finally noticed I'd missed this: https://laravel.com/docs/11.x/broadcasting#listening-for-model-broadcasts If using Model Broadcasting you must prefix the event name with a .. So e.g. PostUpdated becomes .PostUpdated. Or it will fail silently.

Did you find this page helpful?