Skip to content

Samples: Spring Server-Sent Events with Redis

License

Notifications You must be signed in to change notification settings

vpavic/samples-spring-sse-redis

Repository files navigation

Samples: Spring Server-Sent Events with Redis

Build

This repository contains sample applications that showcase Spring Framework’s support for server-sent events backed by Redis Pub/Sub.

The name of each sample application tells the web stack it’s based on, and optionally the data access paradigm it uses to interact with Redis:

  • sample-mvc-imperative: Spring MVC Sample with Imperative Spring Data Redis

  • sample-mvc-reactive: Spring MVC Sample with Reactive Spring Data Redis

  • sample-webflux: Spring WebFlux Sample (with Reactive Spring Data Redis)

Getting Started

Requirements

Java 11 is required to run the samples.

The project is built using Gradle and uses Gradle Wrapper to take care of downloading the appropriate Gradle version.

Running the Samples

Each sample is a Spring Boot based application that can be started:

  • using the bootRun task of the appropriate sample application

  • from IDE by running sample application’s main class

By default, all samples start web server on port 8080 and attempt to connect to Redis running locally at port 6379. In order to change those defaults, use appropriate Spring Boot configuration properties.

All the samples expose the following two HTTP endpoints:

  • POST /topics/{name:[a-z]{2}}

    Generates an event and publishes it to the specified Redis channel. For example, the following request will result in event being published to channel named aa:

    $ curl -s -X POST http://localhost:8080/topics/aa
  • GET /topics/{name:[a-z]{2}}

    Subscribes to server-sent events published to the specified Redis channel. The following request will subscribe to events published to channel named aa:

    $ curl -N http://localhost:8080/topics/aa

Spring MVC Sample with Imperative Redis

This sample uses Spring MVC and a traditional imperative Redis using Jedis driver.

$ ./gradlew :sample-mvc-imperative:bootRun

Spring MVC Sample with Reactive Redis

This sample uses Spring MVC combined with reactive Redis using Lettuce driver.

$ ./gradlew :sample-mvc-reactive:bootRun

Spring WebFlux Sample

This sample uses Spring WebFlux, naturally combined with reactive Redis using Lettuce driver.

$ ./gradlew :sample-webflux:bootRun

Building from Source

The project can be built the build task:

$ ./gradlew build