mallardduck/dynamic-echo-events

A library that allows registering laravel-echo events dynamically and done entirely in PHP.

0.9.0 2020-11-22 18:36 UTC

README

Travis Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version License Coveralls Coverage Status Code Intelligence Status

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.