|
| 1 | +.. index:: |
| 2 | + single: Mercure |
| 3 | + single: Components; Mercure |
| 4 | + |
| 5 | +The Mercure Component |
| 6 | +===================== |
| 7 | + |
| 8 | + `Mercure`_ is an open protocol allowing to push data updates to web |
| 9 | + browsers and other HTTP clients in a convenient, fast, reliable |
| 10 | + and battery-friendly way. |
| 11 | + It is especially useful to publish real-time updates of resources served |
| 12 | + through web APIs, to reactive web and mobile apps. |
| 13 | + |
| 14 | +The Mercure Component implements the "publisher" part of the Mercure Protocol. |
| 15 | + |
| 16 | +Installation |
| 17 | +------------ |
| 18 | + |
| 19 | +.. code-block:: terminal |
| 20 | +
|
| 21 | + $ composer require symfony/mercure |
| 22 | +
|
| 23 | +Alternatively, you can clone the `<https://github.com/symfony/mercure>`_ repository. |
| 24 | + |
| 25 | +.. include:: /components/require_autoload.rst.inc |
| 26 | + |
| 27 | +Usage |
| 28 | +----- |
| 29 | + |
| 30 | +The following example shows the component in action:: |
| 31 | + |
| 32 | + // change these values accordingly to your hub installation |
| 33 | + define('HUB_URL', 'https://demo.mercure.rocks/hub'); |
| 34 | + define('JWT', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InN1YnNjcmliZSI6WyJmb28iLCJiYXIiXSwicHVibGlzaCI6WyJmb28iXX19.LRLvirgONK13JgacQ_VbcjySbVhkSmHy3IznH3tA9PM'); |
| 35 | + |
| 36 | + use Symfony\Component\Mercure\Publisher; |
| 37 | + use Symfony\Component\Mercure\Update; |
| 38 | + use Symfony\Component\Mercure\Jwt\StaticJwtProvide; |
| 39 | + |
| 40 | + $publisher = new Publisher(HUB_URL, new StaticJwtProvide(JWT)); |
| 41 | + // Serialize the update, and dispatch it to the hub, that will broadcast it to the clients |
| 42 | + $id = $publisher(new Update('https://example.com/books/1.jsonld', 'Hi from Symfony!', ['target1', 'target2'])); |
| 43 | + |
| 44 | +Read the full :doc:`Mercure integration documentation </mercure>` to learn |
| 45 | +about all the features of this component and its integration with the Symfony |
| 46 | +framework. |
| 47 | + |
| 48 | +.. _`Mercure`: https://mercure.rocks |
0 commit comments