Reputation: 428
Using Akka-Http and Akka-Streams version 2.4 how can I construct a stream so that it has a sink that is a websocket.
For example I would like to have a source that generates random numbers, these numbers get filtered, lets say keep even numbers, then flow to a WebSocket as a sink.
Thanks
Upvotes: 1
Views: 1514
Reputation: 2401
This code put into a akka http route will output a continuous stream of random numbers without overwhelming the websocket
path("randomNums") {
val src =
Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current.nextInt()))
.filter(i => i > 0 && i % 2 == 0).map(i => TextMessage(i.toString))
extractUpgradeToWebsocket { upgrade =>
complete(upgrade.handleMessagesWithSinkSource(Sink.ignore, src))
}
}
Upvotes: 3