https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

sdeleuze

02/16/2018, 10:07 AM
Hey, what is the recommended way to convert a
ReceiveChannel
to a shared
BroadcastChannel
? I try to convert my Reactor SSE example to Coroutines where I want to use a single stream from my Mongo database to a shared broadcasted stream to browsers. Reactor version: https://github.com/sdeleuze/spring-kotlin-deepdive/blob/step3/src/main/kotlin/io/spring/deepdive/web/ArticleController.kt#L51-L52. Coroutines version without the broadcast feature: https://github.com/sdeleuze/spring-kotlin-deepdive/blob/step3-coroutine/src/main/kotlin/io/spring/deepdive/web/ArticleController.kt#L52-L57.
j

Jonathan

02/16/2018, 10:23 AM
I'm not sure if it is what you want, but what about :
Copy code
private val notificationBroadcast = BroadcastChannel(1).apply {
  launch {
	val count = articleEventRepository.count().toInt()
	articleEventRepository.findWithTailableCursorBy().drop(count).consumeEach {
	  send(it)
	}
  }
}

@GetMapping("/notifications", produces = [(MediaType.TEXT_EVENT_STREAM_VALUE)])
suspend fun notifications(): ReceiveChannel<ArticleEvent> = notificationBroadcast.openSubscription()
s

sdeleuze

02/16/2018, 12:57 PM
Thanks but the content of the channel seems not to be sent to the browser with this version while it is with the
ReceiveChannel
one so I guess something is missing.
5 Views