Skip to content

Commit cf0a338

Browse files
committed
docs(mercure): compatibility with laravel and improve
1 parent 16881ee commit cf0a338

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

core/mercure.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Creating Async APIs using the Mercure Protocol
22

3+
<p class="symfonycasts" style="text-align: center;"><a href="https://symfonycasts.com/screencast/turbo/mercure?cid=apip"><img src="../symfony/images/symfonycasts-player.png" alt="Mercure screencast"><br>Watch the Mercure: Pushing Stream Updates Async screencast</a></p>
4+
35
API Platform can automatically push the modified version of the resources exposed by the API to the currently connected clients (webapps, mobile apps...) using [the Mercure protocol](https://mercure.rocks).
46

57
> _Mercure_ is a protocol allowing to push data updates to web browsers and other HTTP clients in a convenient, fast, reliable and battery-efficient way. It is especially useful to publish real-time updates of resources served through web APIs, to reactive web and mobile apps.
@@ -16,18 +18,22 @@ Then, the Mercure hub dispatches the updates to all connected clients using [Ser
1618
Mercure support is already installed, configured and enabled in [the API Platform Symfony variant](../symfony/index.md).
1719
If you use the distribution, you have nothing more to do, and you can skip to the next section.
1820

19-
If you have installed API Platform using another method (such as `composer require api`), you need to install [a Mercure hub](https://mercure.rocks/docs/getting-started) and the Symfony MercureBundle.
21+
If you installed API Platform using another method (e.g., `composer require api`), you will need to set up the following:
22+
23+
1. A [Mercure hub](https://mercure.rocks/docs/getting-started).
2024

21-
[Learn how to install and configure MercureBundle manually on the Symfony website](https://symfony.com/doc/current/mercure.html)
25+
2. One of the following, depending on your framework:
26+
- For Symfony users: the [MercureBundle](https://symfony.com/doc/current/mercure.html).
27+
- For Laravel users: the [Laravel Mercure Broadcaster](https://github.com/mvanduijker/laravel-mercure-broadcaster).
2228

2329
## Pushing the API Updates
2430

2531
Use the `mercure` attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:
2632

2733
```php
2834
<?php
29-
// api/src/Entity/Book.php
30-
namespace App\Entity;
35+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
36+
namespace App\ApiResource;
3137

3238
use ApiPlatform\Metadata\ApiResource;
3339

@@ -38,8 +44,8 @@ class Book
3844
}
3945
```
4046

41-
Then, every time an object of this type is created, updated or deleted, the new version is sent to all connected clients through the Mercure hub.
42-
If the resource has been deleted, only the (now deleted) IRI of the resource is sent to the clients.
47+
Then, every time an object of this type is created, updated or deleted, the new version is sent to all connected clients
48+
through the Mercure hub. If the resource has been deleted, only the (now deleted) IRI of the resource is sent to the clients.
4349

4450
In addition, API Platform automatically adds a `Link` HTTP header to all responses related to this resource class.
4551
This header allows smart clients to automatically discover the Mercure hub.
@@ -61,8 +67,8 @@ Then, use options to mark the published updates as privates:
6167

6268
```php
6369
<?php
64-
// api/src/Entity/Book.php
65-
namespace App\Entity;
70+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
71+
namespace App\ApiResource;
6672

6773
use ApiPlatform\Metadata\ApiResource;
6874

@@ -73,12 +79,13 @@ class Book
7379
}
7480
```
7581

76-
It's also possible to execute an _expression_ (using the [Symfony Expression Language component](https://symfony.com/doc/current/components/expression_language.html)), to generate the options dynamically:
82+
It's also possible to execute an _expression_ (using the [Symfony Expression Language component](https://symfony.com/doc/current/components/expression_language.html)),
83+
to generate the options dynamically:
7784

7885
```php
7986
<?php
80-
// api/src/Entity/Book.php
81-
namespace App\Entity;
87+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
88+
namespace App\ApiResource;
8289

8390
use ApiPlatform\Metadata\ApiResource;
8491

@@ -104,27 +111,32 @@ In addition to `private`, the following options are available:
104111

105112
## Dispatching Restrictive Updates (Security Mode)
106113

107-
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber with `topic_selector` to receive only publications that are authorized (partner match).
114+
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber
115+
with `topic_selector` to receive only publications that are authorized (partner match).
108116

109-
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber can use the topic selector `https://example.com/books/{id}` as value of the topic query parameter.
110-
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is only authorized to access some of these resources.
117+
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber
118+
> can use the topic selector `https://example.com/books/{id}` as value of the topic query parameter.
119+
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would
120+
> allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is
121+
> only authorized to access some of these resources.
111122
>
112123
> To solve this problem, the mercure.subscribe claim could contain a topic selector such as: `https://example.com/users/foo/{?topic}`.
113124
>
114-
> The publisher could then take advantage of the previously described behavior by publishing a private update having `https://example.com/books/1` as canonical topic and `https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1` as alternate topic.
125+
> The publisher could then take advantage of the previously described behavior by publishing a private update having
126+
> `https://example.com/books/1` as canonical topic and `https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1` as alternate topic.
115127
>
116128
> [https://mercure.rocks/spec#subscribers](https://mercure.rocks/spec#subscribers)
117129
118130
Below is an example using the `topics` option:
119131

120132
```php
121133
<?php
122-
// api/src/Entity/Book.php
123-
namespace App\Entity;
134+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
135+
namespace App\ApiResource;
124136

125137
use ApiPlatform\Metadata\ApiResource;
126138
use ApiPlatform\Api\UrlGeneratorInterface;
127-
use App\Entity\User;
139+
use App\ApiResource\User;
128140

129141
#[ApiResource(
130142
mercure: [
@@ -153,11 +165,11 @@ Using an _expression_ function:
153165

154166
```php
155167
<?php
156-
// api/src/Entity/Book.php
157-
namespace App\Entity;
168+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
169+
namespace App\ApiResource;
158170

159171
use ApiPlatform\Metadata\ApiResource;
160-
use App\Entity\User;
172+
use App\ApiResource\User;
161173

162174
#[ApiResource(
163175
mercure: 'object.getMercureOptions()',

0 commit comments

Comments
 (0)