Hi,
I am trying to setup a simple process where I want to stream data from mongo to my play app using websocket. I am able to get the data through streaming API, but once the data is loaded on the web, the stream gets closed, and it wont take any new inserts/updates in Mongo. My goal is to keep the connection open so any inserts/updates to mongo get reflected in the UI in realtime. Here is how I am doing it:
My websocket:
def ws = WebSocket.accept[Any, String] { _ =>
val src : Future[Source[BSONDocument, Future[State]]] = MongoService.personSource()
val converter : Flow[BSONDocument,String, NotUsed] = Flow[BSONDocument].map(b => b.get("firstName").get.toString)
val sink = Sink.ignore
val result = src.map(s => {
Flow.fromSinkAndSource(sink, s.via(converter))
})
Await.result(result, Duration.Inf)
}
My Mongo Query:
def personSource(): Future[Source[BSONDocument, Future[State]]] ={
val query = BSONDocument.empty
val source = dbFromConnection(connection3).map{ collection =>{
collection.find(query).cursor[BSONDocument].documentSource()
}}
source
}