Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Created March 3, 2021 15:40
Show Gist options
  • Save ManojKiranA/c5573a803ad5f9a73e519705ca90aca5 to your computer and use it in GitHub Desktop.
Save ManojKiranA/c5573a803ad5f9a73e519705ca90aca5 to your computer and use it in GitHub Desktop.
Livewire - React to Server Side Events

Reacting to server side Laravel events (no websockets)

I used a custom directive to implement this, but essentially:

<div wire:new-like="loadLikes">
  
</div>

The new-like event name will listen for any NewLike events fired on the server (needs to be kebab cased since HTML attributes aren't case sensitive), i.e. event(new NewLike) will run the action / evaluate the expression. In this case, when the NewLike event is fired on the server, it will call the loadLikes method on the component.

An example use case might be on a dashboard, where a scheduled command that runs every minute fires an event at the end of each run, but the event is fired conditionally, so it would make more sense than polling every minute, or sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment