YouTubevideo· May 19, 2025· 17:05

Real Time Apps with Laravel Made Easier

Watch on YouTube ↗

transcript

38 moments
  1. 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 ably 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?

  2. 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. Christoph on the official Laravel channel actually put together a video talking about this.

  3. but I wanted to get in and play with it myself as well. And so I did just that and it 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

  4. laravel usually it's underrated because it's just something that you're not thinking of like oh hey that's a first party support kind of thing reverb is a first party broadcasting server that is a part of the laravel ecosystem but broadcasting events is a part a first party part of the laravel ecosystem as well which means yes you have this server side ability to say okay i want to broadcast

  5. 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 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

  6. is just the configuration piece right here. So when 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 just made it with buttons, but this is what you would have in a modern application now. And yes, you could pull the server, but most of the time 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

  7. 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 tool within laravel to be able to send events and to connect to a server like reverb or pusher or ably but then you just make it happen

  8. and i'll show you what that looks like but here's like the particular demo now this is using the default of a queued system for events so it's not going to seem as real time as it might be now you can use the sync driver instead of the queue driver when with any kind of events within laravel so you can say instead of should broadcast which i'll kind of show you in a little bit of what that

  9. looks like within the actual event 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 queue driver and this is

  10. what it does if i was to click on set online for this looks like i messed up the offline and online piece in terms of looks like i have it backwards uh that's okay again don't look at like the the ui of this is mostly just for the implementation of the actual sync events as well so i'm going I'm going to turn this off and you can see here that is turned off.

  11. 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, 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.

  12. And then we have some queue drivers, this queue work app events, toggle online status. Those are queues. You can see on the left side, this purple queue. That's just saying that that queue is being run. So when I run on this side, I say, hey, we're running it, we're running it, running it, running it. A bunch of queues are then being dispatched.

  13. And so those are happening behind the scenes to make these real broadcasting events actually happen. And because this is using WebSockets for Livewire, we're not having any kind of server interaction. There is a server interaction to pull existing or new information if you want it.

  14. 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 going to open this up in my code editor, plop this here. Let's look at the user list.

  15. And this is where all of the magic is happening right now. I have a computer function that grabs all the users. Shout out to Josh Hanley. He's going to be proud of me because I'm using computer functions. I'm trying to use them more, I should say. Now, what we're doing here, and we can actually remove this with function as well. But what we're doing here is we're grabbing all of the users.

  16. and then we're saying when this echo user status the toggle online status actually runs we're just going to get run this 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

  17. changed and there's a bunch of different techniques you could make it to be a little bit more performant this is mostly just to say okay the actual event listening piece livewire 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 is going to be the exact same for both implementations for the react

  18. 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 queue driver and we're just saying that this is creating a new channel a channel to listen to in a web socket connection so that you can say okay anything

  19. 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

  20. have said okay i'm just going to take that payload of a user and status because we're already sending that over the web socket 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 live wire because we're really getting to how it's easy how it's easier now in react or view with the use

  21. 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 so when you create a new let's say you start with a new starter kit if you were to do php artisan install broadcasting it already installs the echo hook for your starter kit in this case if you're using react or view the laravel official starter kits i

  22. should say. But that command should also install React or Vue based off of if you are using a particular React or Vue version of a starter kit as well. But what does this look like now within React? Let's take a look. I'm going to open up my code editor for the React version.

  23. the the simple piece the or the nice piece i should say is in the user's tsx and so a bunch of type script bunch type script 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 a particular i'm grabbing the window.echo and i'm listening for a particular uh you know toggle online status or a user status

  24. channel it just looks and feels like a react version of the livewire on listener event because we're just saying when we get this use echo public or when this user status changes that user status channel and that toggle online status event right here toggle online status that's the app event

  25. 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 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

  26. the GitHub link is in the description below, but I'm doing some fun things with 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 8001, which is the react version. And so this is kind of like an optimistic way of doing this,

  27. where we can it's a little bit it's it's very modern react i should say or i can click a bunch of these you'll see them change really fast on this side but then on the right side 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

  28. going to be sending these events to you know hundreds of thousands of users but you could the the possibilities are really endless but uh if if you just need something like this particular status then a background hue 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

  29. 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 set bam bam you see on the left side it's just changing automatically there's not that cued background information it's just using sync driver to make that happen in the web socket event again the implementation isn't as important

  30. 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 the 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 status as well or we get the payload from that information

  31. we can listen to multiple events and we also have kind of 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 join a channel you can let others know that okay you're now you know you can let them know that you're online instead of having

  32. to send a particular event for my implementation just for all intents and purposes I was just sending a 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 we're just changing the status for the user in the database and then broadcasting that event to others

  33. one quick note this to others i'm it is so nice to have it's basically saying 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

  34. 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 needed to go to everyone else. And this two others is a quick way of doing that. But this use echo hook, this new thing that has been put into

  35. 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 web socket. 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 Christophe's video on the official Laravel channel. I'll link it in the description

  36. below but i just wanted to take a look at what the use hook use echo hooks actually look like for react and view 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

  37. 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 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 They can be created based off of particular users.

  38. You have the ability to create multiple channels and then listen to those particular channels as well within that hook. So how can you use something like this UseEcho 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.

just you here