-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mercure] Add the component #28877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Mercure] Add the component #28877
Conversation
If this component is accepted, I wonder if we should rename it to a more generic name describing its purpose instead of using the name of the provider used. Laravel for example calls this "Broadcasting" and Rails calls it "ActionCable". Thanks! |
@javiereguiluz actually, this is not an abstraction layer but an implementation of the protocol (like HttpFoundation implements the HTTP protocol or WebLink implements Web Linking). |
</xsd:complexType> | ||
|
||
<xsd:complexType name="mercure_hub"> | ||
<xsd:attribute name="name" type="xsd:string" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that one should be marked as required, as it is the key in the map
|
||
<xsd:complexType name="mercure_hub"> | ||
<xsd:attribute name="name" type="xsd:string" /> | ||
<xsd:attribute name="url" type="xsd:string" use="required" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that one should not be required, as it is not required in all XML files being merged together (it is required in the merged config, not in each source config)
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
Show resolved
Hide resolved
private $jwtProvider; | ||
private $httpClient; | ||
|
||
public function __construct(string $publishEndpoint, callable $jwtProvider, callable $httpClient = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest adding some phpdoc describing the expected signature of these callables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly in the format used by Psalm and PHPStan IIRC which have the most chance becoming the standard.
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
Show resolved
Hide resolved
I'm just curious why Mercure should be part of Framework bundle? Why not integrate it by own bundle? |
@s7anley I've no strong opinion about that. I added it to FrameworkBundle because it's what we do most of the time, but not all times (SecurityBundle, WebServerBundle...). Extracting it in a custom bundle will also allow to extract the library out of |
The component, and the related bundle, are now available as standalone packages: |
Last week, I introduced the Mercure protocol as well as a reference implementation of a mercure hub.
Mercure allows to push updates from servers to clients, through a hub. It uses server-sent events, so it passes through all firewalls, works even with very old browsers, is super efficient when using HTTP/2 and is natively supported in all major browsers (no SDK required). Try the demo.
For instance, Mercure allows to automatically and instantly update all currently connected clients (web apps, mobile apps...) every time a resource is modified, created or deleted (e.g. when a
POST
request occurs).This PR introduce a new component, allowing to use Mercure in Symfony projects in a very convenient way.
Minimal example
Here we use the Messenger component to dispatch the update. It means that the request to the Mercure hub can be sent synchronously (by default), or asynchronously in a worker if you configure a transport such as RabbitMQ or Reddis.
The corresponding config:
Subscribing to several topics
Mercure allows to subscribe to several topics, and to topics matching a given pattern:
Making the Hub auto-discoverable
Mercure can leverage the web linking RFC to advertise available hubs to the clients:
In the previous example, the Symfony WebLink Component is used to generate the appropriate
Link
header. The hub can then be discovered using some lines of JS.Requires #28875.
Authorization
Mercure also allows to securely dispatch updates only to users having a specific username, role or group... you name it. To do so, you just have to set a JSON Web Token containing the list of targets in a claim named
mercureTargets
. This token must be stored in a cookie namedmercureAuthorization
(the protocol also allows to transmit the JWT using OAuth, OpenID Connect, and any other transport).That's all! Only users having one of the specified targets will receive the update. In this example,
lcobucci/jwt
is used, but feel free to use any other library to generate the token.To use the authorization feature, the hub and the Symfony app must be served from the same domain name (can be different subdomains).