Events

 
Visual Learner?
Become a pro 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>
    window.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.

use Livewire\Component;

class ShowPosts extends Component
{
    public $addedMessageVisible = false;

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

    public function showPostAddedMessage()
    {
        $this->addedMessageVisible = true;
    }

    public function render()
    {
        return view('livewire.show-posts');
    }
}

Now when any other component on the page emits a postAdded event, this component will pick it up and fire the showPostAddedMessage 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:

use Livewire\Component;

class ShowPosts extends Component
{
    public $addedMessageVisible = false;

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

    ...
}

Passing Parameters

You can also send parameters with an event emission.

$this->emit('postAdded', $post->id);
use Livewire\Component;

class ShowPosts extends Component
{
    public $addedMessageVisible = false;
    public $addedPost;

    protected $listeners = ['postAdded'];

    public function postAdded($postId)
    {
        $this->addedMessageVisible = true;
        $this->addedPost = Post::find($postId);
    }

    public function render()
    {
        return view('livewire.show-posts');
    }
}

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. This is sometimes useful for firing an event in PHP and listening for it in JavaScript.

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>
window.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.
← Previous Topic

Actions

Next Topic →

Lifecycle Hooks