Streaming to Play 2.6 websocket - connection getting closed

21 views
Skip to first unread message

Yaser Arshad

unread,
Sep 9, 2017, 12:29:55 PM9/9/17
to ReactiveMongo - http://reactivemongo.org

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
}

Cédric Chantepie

unread,
Sep 10, 2017, 6:54:33 AM9/10/17
to ReactiveMongo - http://reactivemongo.org
Hi, a MongoDB cursor without awaitData/tailable options won't wait for new documents (anyway that's could only make sense with a capped collection).
Reply all
Reply to author
Forward
0 new messages