Playing
03. Channels and events

Transcript

00:00
OK, let's test creating a channel and broadcasting an event from our Laravel application to that channel. The first thing I'm going to do is run npm run dev.
00:10
So we build all of our assets. Then we're going to go ahead and run our reverb server using the debug option. Finally, we'll create a new tab just so we can issue any commands. OK, so let's talk about our channels first of all.
00:23
So let's head back over to that channels file. Let's say that we're building a global chat application that anyone can join without needing to be authenticated. So it's just a public channel that anyone can listen into.
00:37
Let's go ahead and create that channel out. And then we'll look at how we broadcast to this channel. So let's go ahead and use broadcast channel and let's give this a name. So we're not going to use anything as complex as this just yet.
00:48
We'll just call this chat. Now into the channel, we get a closure in here. We'll always get a current user who is trying to authenticate for this. But in our case, we have a public channel.
01:00
We don't necessarily need a user. So we can just define the channel out like this with nothing inside of here at all. OK, now that we've done that, let's broadcast an event. Now, if you are familiar with Laravel events, you know that you can use PHP
01:15
artisan make event and you can create any kind of event that happens in your application. Events aren't specifically tied to broadcasting. You can have events and listeners within your Laravel application without having to broadcast on them.
01:31
But events will also broadcast things if you tell them that you want them to broadcast something. So let's go ahead and create out an example event. We'll just keep this really simple for now. OK, so we've created an event that lives in app and events.
01:45
So let's go ahead and open up that example event that we've just created and take a look around here. So at the moment, this is just a plain event that we can broadcast from our application. Just to give you some context, if you're new to Laravel, for example, you might
02:01
issue an event when a user has registered and a listener on that event might send them an email. In our case, this example event needs to broadcast a message to our server. Now, let's just take a look down here. We've got a constructor so we can pass any data in here that we need to broadcast
02:16
or use within our event. And down here, you can already see that a broadcast on method has been defined in this event. If you were creating a standard event that didn't need to broadcast, you could just get rid of this method.
02:29
But in our case, we do want to broadcast on a specific channel or multiple channels so we can broadcast to as many channels here as we want because this returns an array. OK, so we have a couple of options for the type of channel that we want to broadcast on. We're going to be covering all of these throughout the course.
02:46
In our case, we want to broadcast on a standard channel, not on a private channel. Now, the namespace is already imported for us, so we can just go ahead and get rid of the private part and we can change this over to the name of the channel that we want to broadcast on, which is chat.
03:02
And we define that in our channels file. OK, so we're not quite ready to broadcast this event yet. There's a really important step here that always catches people out, and that is that we need to implement an interface to actually allow this to be broadcastable.
03:18
Now, there's a couple of options here, and we're going to look at both in this section. We have should broadcast and we have should broadcast now. The difference between these is should broadcast will always put this into a queue. And when you run your queue, this will then, of course, then go ahead and broadcast for
03:37
you. Now, we haven't set up queues at the moment, so we just want to use the should broadcast now interface, implement that. And this event is now ready to be sent. OK, so how are we going to do this?
03:50
Well, let's go over to our routes file and just to find out a simple route that dispatches this event. So we're going to go ahead and copy the home route here. And let's just call this broadcast for the URL.
04:04
And in here, we're just going to broadcast this event. OK, so normally when we dispatch an event, we'd use event or the event helper, and then we would pass a new instance of the thing that we want to go ahead and fire off. In our case, this is a little bit different.
04:19
Instead, we want to go ahead and use the broadcast helper to now broadcast this to our server. So Laravel knows what server we're running because we have that configured. The example event knows which channel to broadcast this to. So when we hit this route, it should just go ahead and broadcast this for us.
04:38
Let's go ahead and take a look now. OK, so let's come over to the browser. I'm just going to register out an account within this starter kit. We're going to need to go over to our dashboard later to listen to this event.
04:49
But we're going to head straight over to the broadcast route that we just defined. Now, remember, before we do that, we have our WebSocket server running. So this will work. If we didn't have this running, we would get an error.
05:04
Let's go ahead and fire this event off and see what happens. OK, so obviously nothing happened here because we're not doing anything. If we head over to here, you can see that we don't actually get this event received at the moment because we're not listening on any of the channels.
05:17
But that message has now been sent to our server. If we just close our server off just to demonstrate this and head back over, you can see that we get an error here because we couldn't connect to our server. So if you see anything like this, just remember to go ahead and run your server.
05:34
OK, so we've created an event. We've chosen which channel we want to broadcast on. Let's go over to our dashboard in the next episode and use Echo to listen to this channel and listen to any events.
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!