You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.


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

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

Method B: From The Component


Method C: From Global JavaScript

2 Livewire.emit('postAdded')

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.

1class ShowPosts extends Component
3 public $postCount;
5 protected $listeners = ['postAdded' => 'incrementPostCount'];
7 public function incrementPostCount()
8 {
9 $this->postCount = Post::count();
10 }

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:

1class ShowPosts extends Component
3 public $postCount;
5 protected function getListeners()
6 {
7 return ['postAdded' => 'incrementPostCount'];
8 }
10 ...
getListeners() will only dynamically generate the names of listeners when the component is mounted. Once the listeners are setup, these can't be changed.

Passing Parameters

You can also send parameters with an event emission.

1$this->emit('postAdded', $post->id);
1class ShowPosts extends Component
3 public $postCount;
4 public $recentlyAddedPost;
6 protected $listeners = ['postAdded'];
8 public function postAdded(Post $post)
9 {
10 $this->postCount = Post::count();
11 $this->recentlyAddedPost = $post;
12 }

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:

1<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:

1$this->emitTo('counter', 'postAdded');
1<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:

1<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:

2Livewire.on('postAdded', postId => {
3 alert('A post was added with the id of: ' + postId);
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:

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

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

2window.addEventListener('name-updated', event => {
3 alert('Name updated to: ' + event.detail.newName);

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

1<div x-data="{ open: false }" @name-updated.window="open = false">
2 <!-- Modal with a Livewire name update form -->
← Previous Topic Actions
Next Topic → Lifecycle Hooks