Download the PHP package bunny-poooh/laravel-mercure-publisher without Composer
On this page you can find all versions of the php package bunny-poooh/laravel-mercure-publisher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bunny-poooh/laravel-mercure-publisher
More information about bunny-poooh/laravel-mercure-publisher
Files in bunny-poooh/laravel-mercure-publisher
Package laravel-mercure-publisher
Short Description Laravel Mercure Publisher
License MIT
Informations about the package laravel-mercure-publisher
Laravel Mercure Publisher
Laravel Mercure Publisher is a simple wrapper around the Symfony Mercure Component, leveraging Server Sent Events with the Mercure protocol.
Installation
This package can be installed through Composer.
The package will automatically register its service provider.
Out of the box, you can define the "Mercure Hub Url" and his "JWT Secret" via Environment Variable in your .env
file:
Optionally, you can publish the entire config file to config/mercure.php
with this command:
Here is the default contents of the configuration file:
Usage
First, make sure you have Mercure installed and running.
Publishing
The Symfony Mercure Component
loaded with this package provides an Update value object representing the update to publish.
You can then use the Publify
service to dispatch updates to the Hub.
The Publify
service can be injected using dependency injection in any other services or controllers:
Subscribing
In your frontend blade
view you can subscribe to updates in JavaScript like this:
Advanced usage
Delegate the JWT generation to your custom service
Instead of directly storing a JWT in the configuration, you can create a service that will return a customized token:
You need to reference this class in the config/mercure.php
configuration file:
Async dispatching
By default, Updates sent with the Publify
service are synchronous.
You can dispatch the updates asynchronously using Laravel Queue. You just have to fill the queue_name
variable in the Mercure configuration file. For example, you can use 'default' or 'high'.
Don't forget to run php artisan queue:work
beforehand.
Mercure Hub Auto-Discovery
This packege auto-register the mercure.discover
Middleware. You can use it on your subscribes routes to inject the URL of the Mercure Hub in a Link
HTTP header.
The hub URL can be automatically discovered:
References
You should also try Laravel Mercure Broadcaster for another type of implementation.
License
This package is published under the MIT License (MIT).