Laravel Mercure Publisher

dev-master / 1.0.x-dev 2019-04-03 15:20 UTC

This package is auto-updated.

Last update: 2021-05-31 00:30:37 UTC


Laravel Mercure Publisher is a simple wrapper around the Symfony Mercure Component, leveraging Server Sent Events with the Mercure protocol.


This package can be installed through Composer.

composer require idplus/laravel-mercure-publisher

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:

php artisan vendor:publish --provider="Idplus\Mercure\MercureServiceProvider"

Here is the default contents of the configuration file:

return [
    'hub' => [
        'url' => env('MERCURE_PUBLISH_URL',''),
        'jwt' => env('MERCURE_JWT_SECRET'),
    'jwt_provider' => null,
    'queue_name' => null,


First, make sure you have Mercure installed and running.


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:

namespace App\Http\Controllers;

use Symfony\Component\Mercure\Update;
use Idplus\Mercure\Publify;

class MercureController extends Controller
    public function pub(Publify $publisher)
        // ...
        $data = ['status' => 'OutOfStock'];
        $update = new Update(
        // ...

        return view('pub', $data);


In your frontend blade view you can subscribe to updates in JavaScript like this:

const es = new EventSource('{{ config('mercure.hub.url') }}?topic=' + encodeURIComponent('http://topic.iri.local/sub/1'));
es.onmessage = e => {
    // Will be called every time an update is published by the server

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:

namespace App;

use Idplus\Mercure\Jwt\JwtProvider;

class CustomJwt implements JwtProvider
     * Return custom JWT
     * @return string
    public function __invoke(): string
        return 'my.custom.jwt';

You need to reference this class in the config/mercure.php configuration file:

    'jwt_provider' => "\App\CustomJwt",

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 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:


namespace App\Http\Controllers;

class MercureController extends Controller
    public function __construct()
        $this->middleware(''); // Auto inject 'Link' header


You should also try Laravel Mercure Broadcaster for another type of implementation.


This package is published under the MIT License (MIT).