YouTubevideo· May 19, 2025· 17:05· 7,700 views on YouTube
React at a moment you like. The best moments light up here.
If you're building or working on any sort of modern application, odds are you're probably going to want to have some kind of real time event handling.
Usually, this means something like Pusher or Aly or Reverb, especially if you're using Laravel. And Laravel even makes using reverb, especially with React or Vue, even easier to listen to those real-time events with a use echo hook. So, what does that look like? I mean, it's already been kind of easy to do this within something like LiveWire.
So, I put together a quick demo to show how you would have done this with LiveWire, how you can still do this with LiveWire, and then how can you do this with something like use Echo Hook within React or Vue. It just makes it easier to listen to those real-time events when you're broadcasting them and not have to think too much about how you're consuming them on the front end.
Kristoff on the official Laravel channel actually put together a video talking about this. Uh, but I wanted to get in and play with it myself as well. And so I I did just that and it's works fantastic. I actually don't know how I would have done this before. I just know it was going to be a lot harder and was a lot harder and is now incredibly easy because you don't have to kind of abstract all of this logic out yourself now. but an incredibly underrated feature of Laravel.
Well, it's probably not underrated by people who use Laravel, but people who don't know about Laravel, usually it's underrated because uh it's just something that you're not thinking of like, oh, hey, that's a firstparty support kind of thing. Reverb is a first-party broadcasting server that is a uh part of the Laravel ecosystem. But broadcasting events is a part a firstparty part of the Laravel ecosystem as well.
Which means yes, you have this server side ability to say, okay, I want to broadcast this particular event, but then you also have this client side ability with echo to say, okay, I'm receiving that event. In the past, you had to do a bunch of weird funky stuff with window.echo echo and then you have to make sure you're receiving that particular event in the window of your application and pushing it to you know whatever page that you needed to especially to receive a particular event name that you're creating in Laravel but
now it just makes it incredibly simple I mean this is just the configuration piece right here so why don't we dive into like what the actual code looks like it's just a really quick demo just to show like online status for example I I just made it made it with button But this is what you would have in a modern application now. And yes, you could pull the server, but most of the times you don't want to do that. So what does it look like with these real-time events? Incredibly simple.
First off, the LiveWire implementation, and I'll kind of show how this is done today. And LiveWire has always made it incredibly simple because it is tightly integrated with with events as well as Echo. So you don't have to think about what you're doing behind the scenes. You just install the broadcasting uh tool within Laravel to be able to send events and to connect to a server like Reverb or Pusher or Aly.
But then you just make it happen. And I'll show you what that looks like. But here's like the particular demo. Now this is using the default of a cued system for events. So it's not going to seem as real time as it might be. Now you can use uh the sync driver instead of the Q driver when with any kind of events within Laravel.
So you can say instead of should broadcast um which I'll kind of show you in a little bit of what that looks like within the actual event uh code you can say should broadcast which just immediately pushes it to the queue or you can say should broadcast now which pushes it to the sync driver which makes it just that much faster.
But if this is a you know a large server maybe you have to think about okay which one's the best for that by default should broadcast which is the Q driver and this is what it does. Um if I was to click on set online uh for this looks like I messed up the offline and online uh piece in terms of looks like I have it backwards.
Uh but that's okay. Again, don't look at like the the UI of this. This is mostly just for the implementation of the actual sync events as well. So, I'm going to turn this off. And you can see here that that is turned off. Jane, this is over here. Turned off over here. And then it is off there. Well, we turn them all on up here. Boom. Boom.
Boom. Boom. And all of a sudden, you see them slowly pop up there. I'm going to turn them offline here. Boom.
Uh, boom, boom, boom, boom. And we can see here on the left all those turned offline. Now they are being sent to the actual queue. You can see here we have a bunch of different app events toggle online status. And then we have some uh Q drivers. This Q work app events toggle online status. Those are Q's. You can see on the left side this purple Q.
That's just saying that that Q is being run. So when I run on this side, I say, "Hey, we're running it. We're running it. We're running it. Running it." A bunch of cues are then being dispatched.
And so those are happening uh behind the scenes to make these real broadcasting events actually happen. And because this is using websockets for live wire, we're not having any kind of server uh inter interaction. Uh there is a server interaction to pull existing or new information if you want it.
But basically LiveWire just gives you the ability to say okay what are you doing when you receive particular information and so that's just kind of waiting for it to happen rather than pulling for that information. So what does this look like in the code? Let's pull it up. So I'm just gonna open this up in my code editor. plop this here. Uh, let's look at the user list. And this is where all of the magic is happening.
Right now, I have a computed function that grabs all the users. Shout out to Josh Hanley.
He's going to be proud of me because I'm using computed functions. I'm trying to use them more, I should say. Now, what we're doing here, and uh we can actually remove this uh with function as well. Uh but what we're doing here is we're grabbing all of the users and then we're saying uh when this echo user
status the toggle online status actually runs we're just going to get uh run this uh function of all users which returns all the users. And of course we could make this a little bit more you know handy. we could only grab the users which online status is you know changed and there's a bunch of different uh techniques you could make it to be a little bit more performant. This is mostly just to say, okay, the actual event listening piece. Live has always made this pretty easy.
This on function, there's other ways to listen for events as well. And then same thing with anytime you're pressing a button or wanting to broadcast, you just say this broadcast thing. This toggle online status. This is going to be the exact same for both implementations. For the React view version and for LineWire version, it's just a simple event that says, okay, this is that should broadcast piece right here. We could change this to should broadcast now, which would use the sync driver, but for should broadcast, it's the Q driver.
And we're just saying that this is creating a new channel, a channel to listen to in a websocket connection so that you can say, okay, anything that is pushed to this user status channel, now I can do what I want with it. And we are setting the ID and status here.
And this is where I didn't get into the full uh optimization of this particular broadcast piece because in user list, I could have instead of grabbing all the users again, I could have said, okay, I'm just going to take that payload of a user and status because we're already sending that over the websocket. And I would just change that on the screen. Again, ignore the actual implementation.
It could be a lot better. This is just showing you what this is doing within LiveWire because we're really getting to how it's easy how it's easier now in React or Vue with the use echo hook. And of course, you could have already made this hook yourself. This is just now already installed when you do the PHP Artisan.
Uh so when you create a new let's say you start with a new starter kit uh if you were to do PHP artisan install broadcasting it already installs the echoh hook for your starter kit in this case if you're using react or view the Laravel official starter kits I should say but that command um should also install uh react or view based off of if you are using a particular react or view version of a starter kit as well.
But uh what does this look like now within uh React? Let's take a look.
I'm going to open up my code editor for the React version. The the simple piece the or the nice piece I should say is in the users TSX. And so uh bunch of TypeScript, bunch of TypeScript, but this is the the bread and butter right here. Use echo public. And so instead of having to think through, okay, I'm listening to particular I'm grabbing the window. And I'm listening for a particular uh, you know, toggle online status or a user status channel.
It just looks and feels like a react version of the live wire on listener event because we're just saying when we get this uh use echo public or when this user status changes the that user status channel and that toggle online status uh event uh right here toggle online status that's the app event. Then all of a sudden we can do what we want. Again, you have the payload of that event.
And here we're getting the ID and the status that is coming from that event and that hook allows us to do that. And then we can set the current users based off of that ID and status. So this I'm doing it the proper way here in React. I'm also doing some fun little things. If you want to take a look at the implementation, the GitHub link is in the description below.
But I'm doing some fun things with the the start transition hook um as well as or the use transition I should say as well as like optimistically being able to load this.
So if we were to pull up uh my browser right here I'm going to go to 81 which is the React version. And so this is kind of like an optimistic way of doing this where we can uh it's a little bit it's it's very modern React I should say where I can click a bunch of these.
You'll see them change really fast on this side but then on the right side uh it's sending those those cues for the background to be able to say that. And again, if you need real real time, then probably that sync driver is going to, you know, going to be handy and you're probably not going to be sending these events to, you know, hundreds of thousands of users. But you could the the possibilities are are really endless.
But, uh, if if you just need something like this particular status, then a background Q driver for these events is probably just as good. So, just to see what that would look like um in our editor, if I was to go into that toggle online status, change that to should broadcast. Now, again, this is using the sync driver, it's going to be incredibly fast. So, if I was to refresh these just to make sure we're we're set. Bam. Bam.
You see on the left side, it's just changing automatically. There's not that cued background uh information. It's just using sync driver to make that happen in the websocket event.
Again, the implementation isn't as important as how easy it is now to use that use echo hook. Uh to say, okay, you can use echo public and I know there's like use echo private, I believe. Let's take a look. So if we were to take a look at the use echo hook uh we basically get the uh status as well or we get the uh payload from that information.
We can listen to multiple events and uh we also have kind of uh different returned functions. So we have leave channel leave stop listening listen being able to kind of have the ability to say okay when certain things happen. This is great for chat rooms.
For example, if you you join a channel, you can let others know that okay, you're now uh you know, you can let them know that you're online instead of having to send a particular event for for my implementation just for all intents and purposes. I was just sending a uh a form request. Where is that?
Right here. Just a form request to say I want to send a different status or change the status. And in my web routes, it's just a simple uh we're just changing the status for the user in the database and then broadcasting that event to others. One quick note, this to others. I'm it is so nice to have. It's basically saying uh I want to send an event live but to everyone besides me.
And this works great for authenticated and authenticated users because you're basically saying uh you know broadcast this actually strike that I don't know if this is actually for uh unauthenticated users as well but for most applications that you're using your users are probably authenticated and this is great to say I don't want to send it to me I don't need to know the thing that I just did to send an event chat message for example I already typed that I need it to go to everyone else and this two others is a quick way of doing that.
But the this use this use echo hook this new uh thing that has been put into Laravel to make it easier is just so clean. It's so nice. I like it. And it makes it just as easy as LiveWire to listen for these things to be able to say, "Okay, I'm getting all the information that's being sent on that websocket."
So, how can I actually do the thing that I need to do within my application for the information that I'm receiving? So, yeah, for a deeper dive, again, check out Christoff's video on the official Laraveville channel. I'll link it in the description below. But I just wanted to take a look at what the use hook use echo hooks actually look like for React and Vue. And I like how it just makes it easier. I was I was already used to listening for those events within LiveWire and it just being easy. It just just works.
And now it's just a lot easier to do things that you want to do.
And I know the Laravel cloud team is using a hook like this because there's so much real-time events happening in Laravel Cloud and really for any modern application that you're doing, you're going to have live events. So, what does it look like to actually, you know, make use of that? And and with the use echo hook, you can have the ability to use it a lot more and a lot more frequently, a lot cleaner of a look, and you don't have to worry about how am I implementing this across my application because it's it's the same from start to finish.
You you just have to listen to a particular channel. And of course, those channels can be, you know, they can be uh created based off of particular users. you have the ability to create multiple channels uh and then listen to those particular channels as well within that hook. So, how are how can you use something like this use echo hook for your next project or just think about the ability to do live broadcasting events real time stuff within your next Laravel project as well. It allows you to keep building and to keep creating.