Playing
11. Listening to multiple events

Transcript

00:00
So far within Echo, we've only been listening to a single event, but we can actually listen to multiple events on a specific channel. Let's go ahead and create out another event to signify when order has been delivered. So let's make out an event here and we'll call this order delivered.
00:17
We'll do everything that we are used to doing. So let's head over to order delivered, make sure that we implement should broadcast now or should broadcast if you're queuing, and we know that we want to accept the same data into here.
00:30
We need the user because we know, need to know which channel is being sent on. And we need to know the order here as well. And when we look at specifically broadcasting on an orders channel, we're not actually going to need to pass this user in later because we'll
00:45
know the user ID from the order. Okay. Let's broadcast on the users channel. So let's say users dot and let's pass in the user ID.
00:57
Like so. Okay, great. So now over in web, we're going to simulate a few delays to these notifications coming through.
01:07
So I'm going to sleep here for three seconds and then down here, I'm going to sleep for five seconds. And then I'm going to send through the new order delivered notification. Okay.
01:18
So now that we're doing that, how do we listen to multiple channels or how do we listen to multiple events on the same channel? Well, we can just chain onto this. It works in exactly the same way.
01:30
So we can grab everything that we've already done, and we can listen to both of these events on this single channel. So this is now the order delivered event. And let's create out a new delivered variable in here and
01:45
also set the order in here. So let's create our delivered and let's go down to our template and we'll create out another if statement to show if something has been delivered, and then we should get both of these notifications and they should both show.
02:02
Okay. So remember we've added that delay. So let's go over and send this through, let that run in the background. And we should, after a few seconds, see the dispatched.
02:12
And then after five seconds, we'll eventually end up with the order has been delivered. So both of these coming through when they happen and contains all of the data you need to display these types of notifications.
18 episodes1 hr 19 mins

Overview

New to realtime broadcasting in Laravel? This course covers the essentials with plenty of examples along the way, leaving you ready to start adding realtime functionality to any of your Laravel applications.

Broken up into channel types, we’ll cover:

  • The basics of installing, configuring and running a Reverb server
  • Broadcasting events
  • Private channels
  • Presence channels
  • Client-to-client broadcasting by whispering
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!