@dunglas
A Modern Push Protocol,
Natively Supported by Symfony & API Platform
@dunglas
Kévin Dunglas
❏ Co-founder of Les-Tilleuls.coop
❏ Creator of Mercure, API Platform and a few more things
❏ Symfony Core Team
@dunglas
@dunglas
Dev/consulting/training - Lille/Paris
✍ Self-managed since 2011
35 people, 1,000% growth in 6 years
👷 ➡ jobs@les-tilleuls.coop
Les-Tilleuls.coop
@dunglas
Push, Push, Push!
@dunglas
Mercure: Push from Server to Clients
❏ Push notifications
❏ Synchronize connected devices in real-time
❏ Notify users when an async task has finished
❏ Collaborative editing (Google Drive-like)
Alternative to WebSocket, socket.io and Pusher
@dunglas
Today’s Use Case: Real-Time UI Updates
1. Some data is changed by a user through a form
2. UIs of all connected devices (webapp,
smartphone…) instantly display updated data
bit.ly/2H17hN3
@dunglas
Try it!
bit.ly/2H17hN3
@dunglas
The Mercure Protocol
@dunglas
@dunglas
Mercure, the Protocol
❏ Full-duplex
❏ Built on top of SSEs and HTTP (POST)
❏ Plays well with techs not supporting persistent connections
(serverless, PHP…)
❏ Authorization mechanism (private updates)
❏ Built-in reconnection support and retrieving of lost messages
❏ Auto-discoverable, designed for REST and GraphQL APIs
❏ Encryption support
@dunglas
Stream events to clients
@dunglas
Internet Draft: draft-dunglas-mercure
@dunglas
Why Server-Sent Events?
❏ Leverage HTTP/2 (WebSocket doesn’t)
❏ High-level: built-in
❏ re-connection
❏ state reconciliation
❏ Native support in modern browsers
❏ Built on top of HTTP
❏ easy to implement
❏ Pass through all firewalls
❏ Connection-less push proxies (mobile)
@dunglas
© Narayan Prusty
What is Multiplexing in HTTP/2?
HTTP/2 Multiplexing
@dunglas
SSEs global support: 89%
@dunglas
There is a Polyfill for That!
SSEs global support (with polyfill): ~100%
@dunglas
The Mercure Hub
@dunglas
The Mercure Hub
❏ Implements 100% of the Mercure protocol
❏ Fast, written in Go
❏ Works everywhere: static binaries and Docker
❏ Automatic HTTP/2 and HTTPS
❏ CORS support, CSRF protection
❏ Cloud Native (12Factor App)
❏ Open source (AGPL)
Managed and on-premise version available!
dunglas+mercure@gmail.com
@dunglas
The Mercure Hub
https://mercure.rocks
@dunglas
Starting the Hub
@dunglas
@dunglas
Using Docker
@dunglas
Using Docker Compose
@dunglas
Publishing
@dunglas
Publishing
@dunglas
Official component and bundle
@dunglas
Official Mercure support in Symfony
@dunglas
Configuration
@dunglas
Publishing: the Mercure Component
@dunglas
Publishing: Messenger Integration
@dunglas
Publishing: the Mercure Component
@dunglas
Subscribing
@dunglas
Subscribing: Basic Usage
@dunglas
Subscribing: Several Topics
@dunglas
Subscribing: URI Templates
@dunglas
The Discovery Mechanism
@dunglas
Discovery Mechanism
40
@dunglas
Discovery: Server-side
@dunglas
Discovery: Client-side
@dunglas
Authorization
@dunglas
Authorization
❏ Uses JSON Web Token (JWT)
❏ An update can be intended for one or several targets
❏ Publisher: must be authenticated
❏ Subscriber:
❏ Can be anonymous (if allowed by the config)
❏ Must be authenticated to receive updates intended for
targets
❏ Two transports: cookie and Authorization header
@dunglas
JSON Web Token and Targets
@dunglas
Publishing or Subscribing to All Targets
@dunglas
Cookie-based Authorization
❏ Set by the app server during the discovery
❏ The app server and the Mercure hub must share
the same domain (or subdomain)
❏ Supported by EventSource
❏ Recommended for web browsers
@dunglas
Setting the cookie with Symfony
@dunglas
HTTP Header-based Authorization
❏ Set by the client
❏ Not supported by native EventSource
❏ Supported by the polyfill
❏ Recommended for servers
@dunglas
Publishing to Specific Targets
@dunglas
Official server-side and client-side support
@dunglas
Mercure Support in API Platform
❏ New in 2.4
❏ Built on top of the Symfony integration
❏ Mercure Hub provided with
❏ the Docker setup
❏ the Kubernetes chart
❏ Ready to use
@dunglas
Auto-dispatch of API Every Updates
POST
PUT
PATCH and
DELETE
will trigger a
Mercure update
@dunglas
@dunglas
Scaffolding a React

Progressive Web App
This skeleton is provide in the full API Platform distribution
@dunglas
Scaffolding a

React Native App
@dunglas
Mercure, SF and Vue.js?
See you in Paris!
@dunglas 62
Thanks! Questions?
@dunglas @coopTilleuls

Official "push" and real-time capabilities for Symfony and API Platform (Mercure protocol)