StreamController.kt

package nl.johnvanweel.kikker

import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import reactor.core.scheduler.Schedulers
import java.time.Duration

@RestController
class StreamController(private val imageSearchService: ImageSearchService) {

    @GetMapping("/stream", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
    fun streamImage(): Flux<Map<String, String>> {
        return Flux.interval(Duration.ZERO, Duration.ofSeconds(10))
            .publishOn(Schedulers.boundedElastic())
            .map { mapOf("update" to imageSearchService.searchRandomImageBase64("frog")) }
    }
}