Events

 
Be amazing at Livewire
with our in-depth screencasts.
Watch Now

Livewire components can communicate with each other through a global event system. As long as two Livewire components are living on the same page, they can communicate using events and listeners.

Firing Events

There are multiple ways to fire events from Livewire components.

Method A: From The Template

<button wire:click="$emit('postAdded')">

Method B: From The Component

$this->emit('postAdded');

Method C: From Global JavaScript

<script>
    Livewire.emit('postAdded')
</script>

Event Listeners

Event listeners are registered in the $listeners property of your Livewire components.

Listeners are a key->value pair where the key is the event to listen for, and the value is the method to call on the component.

class ShowPosts extends Component
{
    public $postCount;

    protected $listeners = ['postAdded' => 'incrementPostCount'];

    public function incrementPostCount()
    {
        $this->postCount = Post::count();
    }
}

Now when any other component on the page emits a postAdded event, this component will pick it up and fire the incrementPostCount method on itself.

If the name of the event and the method you're calling match, you can leave out the key. For example: protected $listeners = ['postAdded']; will call the postAdded method when the postAdded event is emitted.

If you need to name event listeners dynamically, you can substitute the $listeners property for the getListeners() protected method on the component:

class ShowPosts extends Component
{
    public $postCount;

    protected function getListeners()
    {
        return ['postAdded' => 'incrementPostCount'];
    }

    ...
}

Passing Parameters

You can also send parameters with an event emission.

$this->emit('postAdded', $post->id);
class ShowPosts extends Component
{
    public $postCount;
    public $recentlyAddedPost;

    protected $listeners = ['postAdded'];

    public function postAdded(Post $post)
    {
        $this->postCount = Post::count();
        $this->recentlyAddedPost = $post;
    }
}

Scoping Events

Scoping To Parent Listeners

When dealing with nested components, sometimes you may only want to emit events to parents and not children or sibling components.

In these cases, you can use the emitUp feature:

$this->emitUp('postAdded');
<button wire:click="$emitUp('postAdded')">

Scoping To Components By Name

Sometimes you may only want to emit an event to other components of the same type.

In these cases, you can use emitTo:

$this->emitTo('counter', 'postAdded');
<button wire:click="$emitTo('counter', 'postAdded')">

(Now, if the button is clicked, the "postAdded" event will only be emitted to counter components)

Scoping To Self

Sometimes you may only want to emit an event on the component that fired the event.

In these cases, you can use emitSelf:

$this->emitSelf('postAdded');
<button wire:click="$emitSelf('postAdded')">

(Now, if the button is clicked, the "postAdded" event will only be emitted to the instance of the component that it was emitted from.)

Listening For Events In JavaScript

Livewire allows you to register event listeners in JavaScript like so:

<script>
Livewire.on('postAdded', postId => {
    alert('A post was added with the id of: ' + postId);
})
</script>
This feature is actually incredibly powerful. For example, you could register a listener to show a toaster (popup) inside your app when Livewire performs certain actions. This is one of the many ways to bridge the gap between PHP and JavaScript with Livewire.

Dispatching Browser Events

Livewire allows you to fire browser window events like so:

$this->dispatchBrowserEvent('name-updated', ['newName' => $value]);

You are able to listen for this window event with JavaScript:

<script>
window.addEventListener('name-updated', event => {
    alert('Name updated to: ' + event.detail.newName);
})
</script>

AlpineJS allows you to easily listen for these window events within your HTML:

<div x-data="{ open: false }" @name-updated.window="open = false">
    <!-- Modal with a Livewire name update form -->
</div>
← Previous Topic

Actions

Next Topic →

Lifecycle Hooks