mallardduck / dynamic-echo-events
A library that allows registering laravel-echo events dynamically and done entirely in PHP.
Requires
- php: ^7.4|^8.0
- ext-json: *
- composer/composer: ^2.0
- illuminate/contracts: ^7.0|^8.0
Requires (Dev)
- ext-zip: *
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.0
- laravel/framework: ^7.0|^8.0
- mockery/mockery: ^1.3.1
- orchestra/testbench: ^5.0|^6.0
- orchestra/testbench-dusk: ^5.2|^6.0
- php-coveralls/php-coveralls: ^2.4
- phpcompatibility/php-compatibility: *
- phpunit/phpunit: ^8.4|^9.0
- squizlabs/php_codesniffer: *
README
A helper library to dynamically generate the javascript to register Echo event listeners.
Instead of manually writing JS code to register Echo event listeners, simply define the JS handler callback in the Event.
Then add the ImplementsDynamicEcho
contact and use the PrivateDynamicEchoChannel
trait.
Installation
You can install the package via composer:
composer require mallardduck/dynamic-echo-events
You can publish the config file with:
php artisan vendor:publish --provider="MallardDuck\DynamicEcho\DynamicEchoServiceProvider" --tag="config"
This is the contents of the published config file:
return [ 'namespace' => env('DYNAMIC_ECHO_NS', 'App\\Events') ];
Usage
First, you will need to make sure that your site's app.js
file properly configures the Echo javascript client.
This ensures the generated code has the necessary requirements.
Then, modify your sites base theme files to load the dynamic echo generated javascript.
This should be done at a point in the template where the main app.js
is loaded.
Add in:
@dynamicEcho
Finally, when you want an event to automatically register itself in the browser use the Event contact and trait.
This is done simply by adding the ImplementsDynamicEcho
contact and use the PrivateDynamicEchoChannel
trait to the event.
Check the examples
folder for working example(s) of how this is done.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.