Mercury is a WebSocket message broker that enables some simple messaging patterns.
WebSockets are a powerful tool, enabling many features previously impossible, difficult, or ugly for web developers to implement. Where once only an HTTP request could get data from a server, now a persistent socket can allow the server to send updates without the client needing to specifically request it.
WebSockets do not need to be a communication channel purely between browser and server. The Mojolicious web framework has excellent support for WebSockets. Using that support, we can communicate between different server processes. This solves the problem with client-to-client communication in a parallelized web server where all clients may not be connected to the same server process. The server processes can use a central message broker to coordinate and pass messages from one client to another.
A message bus is a many-to-many message pattern. Requesting a
WebSocket from the URL /bus/fry
joins the peer-to-peer
message bus topic fry
. All peers joined to the same
topic will receive all the messages published to that topic by
other peers.
Open another browser window to see messages pass between windows.
Type in a topic and a message and press Enter to send to that topic.
Type in a topic and a message and press Enter to send to that topic.
Requesting a WebSocket from the URL /sub/leela
creates a subscription to the topic "leela". Requesting a WebSocket
from the URL /pub/leela
allows sending messages to the
"leela" topic, which are then received by all the subscribers.
Topics are heirarchical to allow for broad subscriptions without requring more sockets. A subscription to the topic "wong" receives all messages published to the topic "wong" or any child topic like "wong/amy" or "wong/leo"
Type in a topic and press Enter to subscribe to that topic.
Once you're subscribed, type in a topic and a message to send a message on that topic.
Push/pull allows multiple publishers to send messages that will
be handled by exactly one subscriber in a rotation. Requesting a
WebSocket from the URL /pull/bender
creates a
subscription to the topic "bender". Requesting a WebSocket from the
URL /push/bender
allows sending messages to the
"bender" topic, which are then sent to a single subscriber.
Type in a topic and press Enter to subscribe to that topic.
Type in a topic and press Enter to subscribe to that topic.
Once you're subscribed, type in a topic and a message to send a message on that topic.