Skip to content

Repository files navigation

Mongo Change Streams with Server Sent Events in Kotlin

The following project demonstrates how to publish events from a Mongo Change Stream through a Server Sent Event endpoint with SpringBoot and Kotlin.

Overview

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.

Getting started

  1. Spin up the Mongo Replica Set
docker-compose up -d

You can later tear down with:

docker-compose down
  1. Run ServiceA
./gradlew serviceA:bootRun
  1. Run ServiceB
./gradlew serviceA:bootRun

Testing the events

  1. Start listening for events in ServiceB
curl --request GET 'http://localhost:8081/events/stream'
  1. 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"
}'

About

Server Sent Event Demo in a Spring Boot application

Resources

Stars

3 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages