You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/mercure.md
+33-21Lines changed: 33 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
# Creating Async APIs using the Mercure Protocol
2
2
3
+
<pclass="symfonycasts"style="text-align: center;"><ahref="https://symfonycasts.com/screencast/turbo/mercure?cid=apip"><imgsrc="../symfony/images/symfonycasts-player.png"alt="Mercure screencast"><br>Watch the Mercure: Pushing Stream Updates Async screencast</a></p>
4
+
3
5
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).
4
6
5
7
> _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
16
18
Mercure support is already installed, configured and enabled in [the API Platform Symfony variant](../symfony/index.md).
17
19
If you use the distribution, you have nothing more to do, and you can skip to the next section.
18
20
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).
20
24
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).
22
28
23
29
## Pushing the API Updates
24
30
25
31
Use the `mercure` attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:
26
32
27
33
```php
28
34
<?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;
31
37
32
38
use ApiPlatform\Metadata\ApiResource;
33
39
@@ -38,8 +44,8 @@ class Book
38
44
}
39
45
```
40
46
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.
43
49
44
50
In addition, API Platform automatically adds a `Link` HTTP header to all responses related to this resource class.
45
51
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:
61
67
62
68
```php
63
69
<?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;
66
72
67
73
use ApiPlatform\Metadata\ApiResource;
68
74
@@ -73,12 +79,13 @@ class Book
73
79
}
74
80
```
75
81
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:
77
84
78
85
```php
79
86
<?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;
82
89
83
90
use ApiPlatform\Metadata\ApiResource;
84
91
@@ -104,27 +111,32 @@ In addition to `private`, the following options are available:
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).
108
116
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.
111
122
>
112
123
> To solve this problem, the mercure.subscribe claim could contain a topic selector such as: `https://example.com/users/foo/{?topic}`.
113
124
>
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.
0 commit comments