This episode is for members only

Sign up to access "Soketi Basics" right now.

Get started
Already a member? Sign in to continue
Playing
04. Listening to events

Transcript

00:00
Okay then, very quickly, let's go and look at using Laravel Echo to listen on this public channel which will be available for anyone regardless of whether they're authenticated or not. We're going to listen to that specific new thing available event which we're broadcasting from that end point we created.
00:17
So let's go ahead and say Echo which is now on the window object as we saw from over here and we're going to go ahead and say Channel because this is a public channel. And remember we call that Things just to remind us we set the channel name just here. So what we're going to do is we're going to chain on the listen method and we're going to listen to that event. Now this will be the event class name so new thing available.
00:45
We can customize that and that's all in the Laravel documentation. And once that picks up in this event we're going to go ahead and get that event in here which we can just console log out for now. But of course if this was a new post that been published and you'd set the payload of the event in here to the new post then you could take that data and push it somewhere to the page whatever you needed to do with it. Okay so let's go over and just give this a refresh.
01:11
Of course we see nothing at the moment but what we should now see is when we refresh this we get the message instantly broadcasted picked up on the client side and we see that message payload that we pass through to the event. So that's how easy once we've got everything configured is to set up and listen to these events from the back end. So now that we've got public channels down the really important part of this is private channels that require the user to be authenticated. So let's head over and check that out in the next episode.

Episode summary

In this episode, we jump right into using Laravel Echo to listen for events on a public channel. You'll learn how to set up the Echo client and subscribe to a channel—in this case, a channel called things—that anyone can access, whether logged in or not.

We walk through how to use the .channel() and .listen() methods to catch a NewThingAvailable event that's being broadcast from our backend. At first, we're just logging this event to the console, but you'll see how you could easily handle the payload and update your UI when a new event comes in (like displaying a new post, for example).

By the end, you'll see how everything fits together: refresh the page and, once an event fires, it gets picked up in real-time right in your browser console. It's a super quick way to see real-time Laravel broadcasting in action!

Up next, we’ll talk about private channels that require user authentication, so stay tuned if you want to restrict who can listen to your events!

Episode discussion

No comments, yet. Be the first!