A simple example of using Redis PubSub and Spring Reactive Server Side Events for real-time push events to the browser

Spring Boot v2.0.3.RELEASE

Coding using reactive functional style (as opposed to annotation-based)

Spring Initializr Dependencies: “Reactive Web”

Redis Driver Used: Jedis

Language: Kotlin

Scenario and design choices

The scenario described in this article is depicted below:

Reactive Spring: Combining Server-Side Events with Redis PubSub for real-time pu ...
The endgame is to have events published by some 3rd party process to a Redis PubSub channel . Spring is configured to subscribe to this channel, and convert any received events to HTTP Server-sent Events [SSE] which are subsequently sent to the client browser.

Even for such a simple scenario, some planning is required. There are (almost too many) options on how to go about this. For example, you could use Spring Data Redis to interface with redis, rather than directly using the jedis library as I did. You could also go full blown reactive and use the lettuce redis driver instead, which supports reactive programming. See which works best for you, and most importantly which is easier to maintain and understand.

Initial setup

As always, the Spring Framework makes it a breeze to get a functioning reactive webstack up and running.

DefaultRouter.ktdefines a single route (GET /sse) which is handled by the sseEvents method defined in homeHandler.kt .


主题: SpringRedisReactKotlinSSE
本文标题:Reactive Spring: Combining Server-Side Events with Redis PubSub for real-time pu ...

技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(208)