In this episode, we dive into organizing your events a bit better by using namespaced events in your application. Right now, all our events are hanging out in a single folder, but as your app grows, you'll probably want to split them up into subdirectories for clarity—think events like Chat/ExampleTwo
instead of just dumping everything into one place.
We walk through what happens when you move your event into a subdirectory (chat
in our example), and you'll see that if you try to listen for the event the old way, nothing happens! That's because events in subdirectories get namespaced, and you need to reference the full namespace (like App.Events.Chat.ExampleTwo
) when listening, starting with a dot. We show you how to do that, and demonstrate that it works.
There's also a tip on how you can globally change the event namespace using the Echo JS configuration. This can be handy if all your events are in a single directory, but it's generally safer to just use the full namespace for each event. We play around with both options, so you can see how they work.
By the end of this episode, you'll know what "namespaced events" are, why they matter, and how to set them up properly so you’re not wondering why your events aren’t firing anymore!