The following project demonstrates how to publish events from a Mongo Change Stream through a Server Sent Event endpoint with SpringBoot and Kotlin.
The demo includes two independent services:
- ServiceA: Can be used to add items to the database
- ServiceB: Listens for any changes in particular database collection (events)
Change Streams is only supported by replica sets so this demo also includes a docker compose file to spin up a replica set that can be used for this example.
- Spin up the Mongo Replica Set
docker-compose up -dYou can later tear down with:
docker-compose down- Run ServiceA
./gradlew serviceA:bootRun- Run ServiceB
./gradlew serviceA:bootRun- Start listening for events in ServiceB
curl --request GET 'http://localhost:8081/events/stream'- Publish an event through ServiceA by adding an event to the database:
curl --request POST 'http://localhost:8080/events' \
--header 'Content-Type: application/json' \
--data-raw '{
"inStage": "BODY",
"outStage": "PAINT",
"productId": "String",
"info": "String"
}'
