The purpose of this demo is to show how to use websockets to publish and subscribe events (useful in an Event Driven Architecture - EDA - based system).
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection (see https://en.wikipedia.org/wiki/WebSocket).
In this demo, we use 3 concepts: event (and its eventCode
), publication, subscription.
-
an event is a message sent from a publication to multiple subscriptions. An event has an
eventCode
which is the category of the event. AneventCode
is for exampleFRAUD_DETECTED
orTRANSFER_COMPLETED
orEUR_USD_UPDATED
... An event has only oneeventCode
but multiple events can be sent with the sameeventCode
. -
a publication sends events. A publication sends events of a given category (events with a given
eventCode
). A publisher - a component of the system - can use multiple publications, with one publication for each category of events. Publications do not know subscriptions. The ony thing that publications and subscriptions share are the event categories (theeventCode
). -
a subscription receives events. A subscription subscribes and receives events of a given category (events with a given
eventCode
). A subscriber - a component of the system - can use multiple subscriptions, with one subscriptions for each category of events. Subscriptions do not know publications. The ony thing that publications and subscriptions share are the event categories (theeventCode
).
This demo uses Quarkus (www.quarkus.io) (and not SpringBoot). No special reason for that; it's just because I wanted to play with Quarkus !
Build and start the broker:
cd broker
../mvnw clean package
java -jar target/events-websockets-quarkus-broker-1.0-SNAPSHOT-runner.jar
In a terminal, start a publication for events of category eventA
:
wscat -c localhost:8080/events/eventA/publications
In another terminal, start a first subscription for events of category eventA
:
wscat -c localhost:8080/events/eventA/subscriptions
In another terminal, start a second subscription for events of category eventA
:
wscat -c localhost:8080/events/eventA/subscriptions
In another terminal, start a publication for events of category eventB
:
wscat -c localhost:8080/events/eventB/publications
In another terminal, start a first subscription for events of category eventB
:
wscat -c localhost:8080/events/eventB/subscriptions
In another terminal, start a second subscription for events of category eventB
:
wscat -c localhost:8080/events/eventB/subscriptions
In the terminal of the publication for events of category eventA
, type a message.
Check the message is displayed in the terminals of the two subscriptions for events of category eventA
.
In the terminal of the publication for events of category eventB
, type a message.
Check the message is displayed in the terminals of the two subscriptions for events of category eventB
.
wscat
is a NodeJS utility used to send and display messages sent/received via websockets (and not regular HTTP REST/JSON API).
To install wscat, run:
sudo npm install -g wscat
To run wscat, run:
wscat -c ws://echo.websocket.org
Replace
ws://echo.websocket.org
by the actual endpoint of you websocket
The broker runs on the port
8080
cd broker
../mvnw compile quarkus:dev
cd broker
../mvnw clean package
java -jar target/events-websockets-quarkus-broker-1.0-SNAPSHOT-runner.jar
In a terminal, start a publication for events of category eventA
:
wscat -c localhost:8080/events/eventA/publications
In another terminal, start a subscription for events of category eventA
:
wscat -c localhost:8080/events/eventA/subscriptions
Type some message in the terminal of the publication and check that the message is diplayed in the terminal of the subscription.
The publisher runs on the port
8081
cd publisher
../mvnw compile quarkus:dev
cd publisher
../mvnw clean package
java -jar target/events-websockets-quarkus-publisher-1.0-SNAPSHOT-runner.jar
Publish an event using curl:
curl -d '{"eventCode":"eventA", "payload":"helloA"}' -H "Content-Type: application/json" -X POST http://localhost:8081/events
Publish an event using a browser (from the navigation bar of the browser):
http://localhost:8081/events/eventA/publications?payload=helloA
After publishing the event, look at the logs displayed in the terminal(s) of the subscription(s).
You should start the subscriber before the publisher otherwise you will not be able to see the event in the logs of the subscriber !
The subscriber runs on the port
8082
cd subscriber
../mvnw compile quarkus:dev
cd subscriber
../mvnw clean package
java -jar target/events-websockets-quarkus-subscriber-1.0-SNAPSHOT-runner.jar
Subscribe to an event category using curl:
curl -X POST http://localhost:8082/events/eventA/subscriptions
Subscribe to an event category using a browser (from the navigation bar of the browser):
http://localhost:8082/events/eventA/subscriptions