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
+31-21Lines changed: 31 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -16,18 +16,22 @@ Then, the Mercure hub dispatches the updates to all connected clients using [Ser
16
16
Mercure support is already installed, configured and enabled in [the API Platform Symfony variant](../symfony/index.md).
17
17
If you use the distribution, you have nothing more to do, and you can skip to the next section.
18
18
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.
19
+
If you installed API Platform using another method (e.g., `composer require api`), you will need to set up the following:
20
20
21
-
[Learn how to install and configure MercureBundle manually on the Symfony website](https://symfony.com/doc/current/mercure.html)
21
+
1. A [Mercure hub](https://mercure.rocks/docs/getting-started).
22
+
23
+
2. One of the following, depending on your framework:
24
+
- For Symfony users: the [MercureBundle](https://symfony.com/doc/current/mercure.html).
25
+
- For Laravel users: the [Laravel Mercure Broadcaster](https://github.com/mvanduijker/laravel-mercure-broadcaster).
22
26
23
27
## Pushing the API Updates
24
28
25
29
Use the `mercure` attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:
26
30
27
31
```php
28
32
<?php
29
-
// api/src/Entity/Book.php
30
-
namespace App\Entity;
33
+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
34
+
namespace App\ApiResource;
31
35
32
36
use ApiPlatform\Metadata\ApiResource;
33
37
@@ -38,8 +42,8 @@ class Book
38
42
}
39
43
```
40
44
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.
45
+
Then, every time an object of this type is created, updated or deleted, the new version is sent to all connected clients
46
+
through the Mercure hub. If the resource has been deleted, only the (now deleted) IRI of the resource is sent to the clients.
43
47
44
48
In addition, API Platform automatically adds a `Link` HTTP header to all responses related to this resource class.
45
49
This header allows smart clients to automatically discover the Mercure hub.
@@ -61,8 +65,8 @@ Then, use options to mark the published updates as privates:
61
65
62
66
```php
63
67
<?php
64
-
// api/src/Entity/Book.php
65
-
namespace App\Entity;
68
+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
69
+
namespace App\ApiResource;
66
70
67
71
use ApiPlatform\Metadata\ApiResource;
68
72
@@ -73,12 +77,13 @@ class Book
73
77
}
74
78
```
75
79
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:
80
+
It's also possible to execute an _expression_ (using the [Symfony Expression Language component](https://symfony.com/doc/current/components/expression_language.html)),
81
+
to generate the options dynamically:
77
82
78
83
```php
79
84
<?php
80
-
// api/src/Entity/Book.php
81
-
namespace App\Entity;
85
+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
86
+
namespace App\ApiResource;
82
87
83
88
use ApiPlatform\Metadata\ApiResource;
84
89
@@ -104,27 +109,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).
112
+
Use `iri` (iriConverter) and `escape` (rawurlencode) functions to add an alternative topic, in order to restrict a subscriber
113
+
with `topic_selector` to receive only publications that are authorized (partner match).
108
114
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.
115
+
> Let's say that a subscriber wants to receive updates concerning all book resources it has access to. The subscriber
116
+
> can use the topic selector `https://example.com/books/{id}` as value of the topic query parameter.
117
+
> Adding this same URI template to the mercure.subscribe claim of the JWS presented by the subscriber to the hub would
118
+
> allow this subscriber to receive all updates for all book resources. It is not what we want here: this subscriber is
119
+
> only authorized to access some of these resources.
111
120
>
112
121
> To solve this problem, the mercure.subscribe claim could contain a topic selector such as: `https://example.com/users/foo/{?topic}`.
113
122
>
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.
123
+
> The publisher could then take advantage of the previously described behavior by publishing a private update having
124
+
> `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