Couldn't get this websocket example to work, any suggestions?

28 views
Skip to first unread message

Kaiyin Zhong

unread,
Apr 3, 2018, 8:09:48 AM4/3/18
to ReactiveMongo - http://reactivemongo.org
Dear list,

I have some trouble working out this websocket example: https://github.com/kindlychung/Play-Reactivemongo-Akka-WebSocket

The basic idea is to listen to a certain mongodb collection and once there is a new element added, show it on the webpage.


Every time a new element is added to the collection, the ReceiveUpdate message is supposed to be sent to UserListenActor, but unfortunately that is not the case here.

Could anyone help?

Thanks.

Cédric Chantepie

unread,
Apr 3, 2018, 12:01:31 PM4/3/18
to ReactiveMongo - http://reactivemongo.org
First test the repository source alone.

Kaiyin Zhong

unread,
Apr 4, 2018, 9:11:44 AM4/4/18
to ReactiveMongo - http://reactivemongo.org
Could you elaborate a bit? How can I test the repository alone?

I tried to set up a naive app with UserRepository, but it gives me NPE. It seems difficult to run it as anything other than a play app.

Kaiyin Zhong

unread,
Apr 5, 2018, 4:55:51 AM4/5/18
to ReactiveMongo - http://reactivemongo.org
So here is running the repository code alone:

package repository

import javax.inject.Inject
import reactivemongo.api._
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, scaladsl}
import akka.stream.scaladsl.Source
import models._
import play.api.Logger
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.api.commands.WriteResult
import reactivemongo.bson.{BSONDocument, Macros}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.ExecutionContext.global
import scala.util.{Failure, Success, Try}

trait UserRepositoryT {
import reactivemongo.akkastream._
implicit val system: ActorSystem = ActorSystem()
implicit val mat: ActorMaterializer = ActorMaterializer()

implicit def userWriter = Macros.writer[User]
implicit def userReader = Macros.reader[User]
implicit def ec: ExecutionContext
val uri = "mongodb://localhost:27017/playwebsocketdemo"
val dbname = "playwebsocketdemo"
val collectionName = "users"
val driver = MongoDriver()
val parsedUri: Try[MongoConnection.ParsedURI] = MongoConnection.parseURI(uri)
val connection: Try[MongoConnection] = parsedUri.map(driver.connection)
lazy val db: Future[DefaultDB] = connection match {
case Success(conn) =>
println(s"==== Connection: $conn")
conn.database(dbname)
case Failure(e) => throw new Exception(s"failed to connect to mongodb $uri")
}
lazy val collection: Future[BSONCollection] = db.map(_.collection(collectionName))

def create(user: User): Future[WriteResult] = {
collection.flatMap(_.insert(user))
}

private def createCursor: Future[AkkaStreamCursor[User]] = {
collection.map(_.find(BSONDocument())
.options(QueryOpts().tailable.awaitData)
.cursor[User]())
}

def listenUserCollection(f: User => Unit): Unit = {
val c = createCursor
c.onComplete {
case Success(stream) =>
Logger.info("Document source stream created.")
val s: Source[User, Future[State]] = stream.documentSource()
s.runForeach(f)
case _ =>
Logger.info("Failed to create cursor.")
}
}
}

class UserRepository @Inject()(implicit val ec: ExecutionContext) extends UserRepositoryT

object UserRepository extends App with UserRepositoryT {
override val ec = global
listenUserCollection { user =>
Logger.info(s"==== Created new user: $user")
}
create(User("xxx", "jo", "j...@j.org"))
}



The expected behavior is that when a user is added, it will be logged in the console, but that didn't happen.

Any ideas?





On Tuesday, April 3, 2018 at 6:01:31 PM UTC+2, Cédric Chantepie wrote:

Cédric Chantepie

unread,
Apr 5, 2018, 5:05:47 PM4/5/18
to ReactiveMongo - http://reactivemongo.org
I mean test the repository in an isolated way. For now there are too many things that should be checked.

Kaiyin Zhong

unread,
Apr 6, 2018, 4:59:57 AM4/6/18
to reacti...@googlegroups.com
Never mind, I managed to make it work yesterday. The reason why it was not working is that the users collection was not capped.

Thanks for your input in any case.


Best regards,

Kaiyin ZHONG


2018-04-05 23:05 GMT+02:00 Cédric Chantepie <chantep...@gmail.com>:
I mean test the repository in an isolated way. For now there are too many things that should be checked.

--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/Pkbp04e_dH0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemongo+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages