In the past few weeks, I discovered Mercure, the first time I tested it for a POC and realized that this is the beginning of a new era in real-time application development.
I just attended Symfony Live Paris and Kévin Dunglas the creator of Mercure made a very interesting Talk about Mercure and API-Platform, I told myself it will be cool to share the tool with dev.to family, especially that Mercure is officially supported by Symfony Mercure Component.
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 a reactive web and mobile apps.
WebSocket is a low-level protocol, Mercure is a high level one. Mercure provides convenient built-in features such as authorization, re-connection, and state reconciliation; while with WebSocket, you need to implement them yourself. Also, unlike Mercure (which is built on top of HTTP and Server-Sent Events), WebSocket is not designed to leverage HTTP/2.
HTTP/2 connections are multiplexed and bidirectional by default (it was not the case of HTTP/1). When using Mercure over a h2 connection (recommended), your app can receive data through Server-Sent Events, and send data to the server with regular
DELETE) requests, with no overhead.
Basically, in most cases Mercure can be used as a modern and easier to use replacement for WebSocket
WebSub is a server-to-server only protocol, while Mercure is also a server-to-client and client-to-client protocol.
Mercure has been heavily inspired by WebSub, and we tried to make the protocol as close as possible from the WebSub one.
Mercure uses Server-Sent Events to dispatch the updates, while WebSub uses
POST requests. Also, Mercure has an advanced authorization mechanism and allows to subscribe to several topics with only one connection using URI templates.