Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eugenefvdm's avatar

Trigger JavaScript on a page (e.g. Toast notification) via API Callback - can it be done with Livewire?

I'm not understanding the interaction between Livewire, Laravel PHP, JavaScript events, and broadcasting.

For my application I want to trigger JavaScript on a page, say for example show a Toast notification, when I receive a post request from a 3rd party website. I believe I can do this with laravel-sockets and Laravel Echo but the overhead seems huge just to get notifications working.

I guess the part I don't exactly understand is how would a PHP backend (from an arbitrary page or route) communicate directly to an HTML page and trigger JavaScript on that page...

Longer explanation:

  1. My supplier give me JavaScript to launch an iFrame that connects to the Bank.

To launch the iFrame the supplier's JavaScript header is this:

bankLinkBtn.addEventListener('click', function() {
                window.banklink.open({
                        fastLinkURL: '<?php echo $bankLinkURL; ?>',
                        jwtToken: 'Bearer <?php echo $token; ?> ',
                        params: {
                            workflowFlow: 'Consolidation',
                            callback: '<?php echo $callback; ?>',
                        },
                        onSuccess: function(data) {                            
                            console.log(data);
                        },
                        onError: function(data) {                            
                            console.log(data);
                        },
...
  1. The route in my web application is this:
Route::get('/bank/callback', [Controllers\BankApiController::class, 'callback'])->name('bank.callback');

What I want to do in the callback method on the Bank API Controller is to refer back to the original JavaScript page and trigger a toast notification, say "Linking complete!".

So the direct path that seems unclear in my mind is:

Can one set an API route direct to a Livewire component page?

Can I "anywhere" on my Laravel app "contact" this Livewire page and do something with the JavaScript?

Or do I have to use Websockets and event broadcasting?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

probably easiest is to have a component on the page that uses wire:poll to check the status of the account. When the status changes to linked (or whatever) you can show a confirmation on the page. Far easier than broadcasting which is more for sending the same content to multiple users. Since your user is sat there waiting for the linking, you can use polling.

1 like
eugenefvdm's avatar

@Snapey I just tried it. The problem is that JavaScript loads an iFrame. So every time the polling kicks in the iFrame disappears...

EDIT: Actually your advice is spot on. I added another livewire component which sole purpose is to do the polling so now the iFrame stays.

But the key to your answer lies in the Livewire documentation about polling:

Polling for changes over Ajax is a lightweight, simpler alternative to something like Laravel Echo, Pusher, or any WebSocket strategy.

https://laravel-livewire.com/docs/2.x/polling

Thanks!

Please or to participate in this conversation.