DEV Community

Zairig Imad
Zairig Imad

Posted on

Mercure (Simple Real-Time Updates)

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.

What is Mercure?

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.

Mercure

What's the Difference Between Mercure and WebSocket?

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 POST (or PUT/PATCH/DELETE) requests, with no overhead.

Basically, in most cases Mercure can be used as a modern and easier to use replacement for WebSocket

What's the Difference Between Mercure and WebSub?

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.

I invite you to try the Demo Application and the Full Documentation in The Github Repository
https://github.com/dunglas/mercure

Top comments (0)