Playing
07. Namespaced events

Transcript

00:00
It's likely at some point you're going to want to organize the events within your
00:03
application in a much better way at the moment. We just have our events folder here and we have the example event just within the root namespace of this events folder. Now, what happens if we want to go ahead
00:17
and put this inside a different directory? Well, let's go ahead and try that out and see what happens. Well, let's go ahead and create out another event here and we'll call this example two, but we're going to put this inside of a directory here.
00:30
So let's put this inside of a chat directory within our events folder. OK, so now that we've done this, what's going to happen if we just set this to broadcast? So remember, implement should broadcast now
00:45
and then choose the channel so we can actually connect to it. So we'll just do the same thing. We'll just say chat. What's going to happen when we try and pick this event up?
00:54
Well, let's go over to our web routes and we'll switch this up to the example two. We'll just get rid of this one temporarily and put this one up here. So let's say example two, which now comes from that chat directory. And we're going to go over to our dashboard and listen for this event.
01:11
So let's head back over to our dashboard and let's listen for example two. OK, let's go back over to the browser and we'll fire this event off and sure enough, nothing happens. So when you're dealing with events
01:24
that are namespaced, there's a certain convention that you have to use. If your event is just directly within the events directory, you can do what we've been doing so far and just to find out the event name. But if it's within a subdirectory,
01:39
we need to provide the full path to this namespace and we start this off with a dot. So we're going to say app events. Chat and then example two. Now, there's an easier way to define this out.
01:53
I'll show you that in just a second. But let's have a look at the difference that this is made. So let's broadcast this, come over and there we go. We get that event through.
02:02
Now, you can actually configure the namespace, the prefix of the namespace directly using Echo. So if we just come over to Echo JS and we add in the namespace option in here, we can define this out with the namespace that we expect.
02:17
Now, this will be set globally. So it's not a great idea unless you have your events in a completely separate directory. So, for example, if you're not following Laravel's event structure with this folder inside of app and that was
02:30
in a different directory, this would be a good reason to change it. Or if all of your events were in a single directory with inside of events, you could also use this technique. To do this, we just use app dot and then events and then dot chat.
02:46
So we can define out the namespace like this and that will set that to the root. We're going to get rid of this because we have events that are outside of this directory, like example, and then this won't work. But you can do this if they're in a
03:00
completely separate location in your application. OK, so let's go back over to our dashboard and let's change this back over to just example two. Now that we've defined that root
03:10
namespace and let's go over and run this again. And there we go. You can see that we still get the event through. So you've got a couple of options to do this,
03:17
but usually just providing the full namespace to it makes a lot more sense. OK, let's go back over to Echo and just get rid of this temporarily. And there we go. We've now looked at namespaced 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!