@dunglas - mercure.rocks
Real-Time APIs for Serverless and Beyond
Slide 2
Slide 2 text
@dunglas - mercure.rocks
Kévin Dunglas
❏ Founder of Les-Tilleuls.coop
❏ Creator of Mercure, API Platform…
❏ Symfony Core Team
@dunglas
Slide 3
Slide 3 text
@dunglas - mercure.rocks
API and cloud experts
✊ Self-managed, 100% employee-owned
40 people, 1,000% growth in 6 years
[email protected]
Les-Tilleuls.coop
Slide 4
Slide 4 text
@dunglas - mercure.rocks
Real-Time APIs?!
Slide 5
Slide 5 text
@dunglas - mercure.rocks
Mercure: Push from Server to Clients
❏ Push notifications
❏ Synchronize connected devices in real-time
❏ Collaborative editing (Google Docs-like)
❏ Notify users when an async task has finished
Modern and high level alternative to WebSocket
@dunglas - mercure.rocks
WebSocket (RFC 6455)
Benefits over SSE:
❏ Full-duplex
communication
❏ Low level: full control
Drawbacks over SSE:
❏ Low level: no native
❏ auth
❏ re-connection
❏ state reconciliation
❏ events history
❏ Obsoleted by HTTP/2 & 3
❏ Hard to secure
Slide 10
Slide 10 text
@dunglas - mercure.rocks
The Persistent Connections Problem
❏ WebSocket and SSE rely on persistent connections
❏ Serverless platforms (AWS Lambda, Cloud Run, Azure
functions…), PHP, FastCGI…
are designed for short-lived connections
❏ Persistent, long-lived, concurrent connections are better
handled by
dedicated and optimized software and hardware
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
@dunglas - mercure.rocks
The Mercure Protocol
Slide 13
Slide 13 text
@dunglas - mercure.rocks
Slide 14
Slide 14 text
@dunglas - mercure.rocks
Mercure, at a Glance
❏ Full-duplex, but plain old HTTP
❏ Publish: HTTP POST
❏ Subscribe: SSE
❏ Built-in: reconnection, retrieving of lost messages, history
❏ Auto-discoverable: designed for REST and GraphQL
❏ JWT-based authorization mechanism (private updates)
❏ Designed for serverless, PHP, FastCGI…
❏ End-2-End encryption support
Slide 15
Slide 15 text
@dunglas - mercure.rocks
Stream Events to Clients
Slide 16
Slide 16 text
@dunglas - mercure.rocks
Internet Draft: draft-dunglas-mercure
@dunglas - mercure.rocks
HTTP/2 support: 92% of all users
...and SSEs also work with HTTP/1.x
Slide 21
Slide 21 text
@dunglas - mercure.rocks
SSE support: 93% of all users
and there is a polyfill for IE 5.5+
Slide 22
Slide 22 text
@dunglas - mercure.rocks
Publishing
Slide 23
Slide 23 text
@dunglas - mercure.rocks
Publishing
Slide 24
Slide 24 text
@dunglas - mercure.rocks
Subscribing
Slide 25
Slide 25 text
@dunglas - mercure.rocks
Subscribing: Basic Usage
Slide 26
Slide 26 text
@dunglas - mercure.rocks
Subscribing: Several Topics
Slide 27
Slide 27 text
@dunglas - mercure.rocks
Subscribing: URI Templates
Slide 28
Slide 28 text
No content
Slide 29
Slide 29 text
@dunglas - mercure.rocks
The Discovery Mechanism
Slide 30
Slide 30 text
@dunglas - mercure.rocks
Discovery Mechanism
Web Linking: RFC 5988
Slide 31
Slide 31 text
@dunglas - mercure.rocks
Authorization
Slide 32
Slide 32 text
@dunglas - mercure.rocks
Authorization
❏ Uses JSON Web Token (JWT - RFC 7519)
❏ 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 private updates
❏ Two transports: cookie and Authorization header
Slide 33
Slide 33 text
@dunglas - mercure.rocks
Slide 34
Slide 34 text
@dunglas - mercure.rocks
The Mercure Hub
Slide 35
Slide 35 text
@dunglas - mercure.rocks
Reference Implementation
❏ Implements 100% of the Mercure protocol
❏ Fast, written in Go
❏ Works everywhere: static binaries and Docker
❏ Automatic HTTP/2 and HTTPS (Let’s Encrypt)
❏ CORS support, CSRF protection
❏ Cloud Native (12Factor App)
❏ Open source (AGPL)
❏ Optional: a server can implement directly the protocol
@dunglas - mercure.rocks
Starting the Hub
https://mercure.rocks
Slide 38
Slide 38 text
@dunglas - mercure.rocks
On a Kubernetes Cluster
Slide 39
Slide 39 text
@dunglas - mercure.rocks
Alternative Implementations
❏ No hubs: the hub is optional
❏ Go library (AGPL)
❏ Node.js library (GPL)
❏ HA version (managed and on premise)
in private beta: [email protected]
Slide 40
Slide 40 text
@dunglas - mercure.rocks
iGraal / Glory4Gamers Benchmarks
❏ Open Source version (EC2 t3.micro)
❏ 40k concurrent connections
❏ HA version (on premise)
❏ 200k concurrent connections
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
@dunglas - mercure.rocks
Integrations
Slide 43
Slide 43 text
@dunglas - mercure.rocks
Integrations
❏ Official integrations:
❏ Symfony, API Platform
❏ Community integrations:
❏ Laravel
❏ Official examples:
❏ JS, Python, Go, Ruby, PHP
Slide 44
Slide 44 text
@dunglas - mercure.rocks
Example using
API Platform & React
Slide 45
Slide 45 text
@dunglas - mercure.rocks
Create a Mercure-enabled Web API
api-platform.com
Slide 46
Slide 46 text
You get a fully-featured API supporting:
❏ JSON-LD + Hydra + schema.org
❏ GraphQL, JSON:API, HAL
❏ OpenAPI
❏ auth, pagination, filters, validation,
HTTP/2 push, test framework…
❏ and Mercure support!
MIT license