[10.x] Add a "channel:list" command #46248
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey!
This PR proposes a new
php artisan channel:list
Artisan command that can be used to list the registered broadcasting channels in your app.What is it?
My idea behind this is that I find the
route:list
command really useful. I sometimes end up working on projects that have a big-ish number of channels registered in theroutes/channels.php
file. So I tried to mimic the concept of theroute:list
command for broadcasting channels.I think this command could possibly make it easier to get a quick overview of which private channels are registered in the application (in alphabetical order).
One thing I also like about the command is that I've added a warning message that lets you know if you've not uncommented the
BroadcastServiceProvider
in yourconfig/app.php
file. I don't know about other people, but this is something I tend to forget to do quite often when adding WebSockets to my applications haha! I then spend quite a while trying to figure out why I keep getting a 404 when trying to authorise a private channel using Echo. I'd like to think that this would reduce the chance of that happening.So far, I've only done a limited amount of testing, so there may be some things I've missed. I've also not written any tests for it just yet, just in case it wasn't something you thought would be worth merging. If it's something you think might be worthwhile, I'll be happy to put some tests together.
Future and limitations
At the moment, the command is relatively basic and just outputs the name of the channel and the channel class or closure that is used to resolve the authorisation logic.
I think this command could be extended in the future to have things like filtering (similar to the
route:list
command).It could maybe also list the parameters that are expected by the closure/class (e.g. - an
App\Models\Chat
model).I'm a little biased, but I think this command could be a handy little tool to improve the DX when working with broadcasting. I think it has potential to be extended with useful features.
One limitation I'm aware of is that it doesn't list the public channels because they're not listed in the
routes/channels.php
file. I don't know if it's possible to extend the command in the future to also detect other public channels that are being used in the event classes?I also guessed at the design for the command and used the same approach as the
route:list
command. But I'm wondering if it would be better to make it more like (with the dots separating and the "resolver" being on the right side of the terminal):Example screenshots
This screenshot shows what happens if there aren't private channels registered in the app:
This screenshot shows what happens if the provider isn't registered and there are no private channels registered:
This screenshot shows what happens if there are 4 channels registered:
If this might be something you'd be interested in accepting, please let me know if there's anything you'd like changing. Thanks! 😄