简体   繁体   中英

How to get real-time communication between JavaScript and Scala

I am trying to find best solution to communicate from Scala to JavaScript in real-time.
So far tested couple ideas, but given library must handle around 1000 requests per second, and not every solution is best.
Below attached one of the methods. if (modulo == 0) statement needs some fast library to push events to frontend.

What do you think about vert.x and its pub sub library? https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/pubsub .
And could this library handle 1000 request per second? Same question with this https://github.com/vert-x/vertx-examples/tree/master/src/raw/scala/websockets I was trying this push messaging library https://www.scaledrone.com/ but it failed at the beginning (just 10 request per seconds).

Or maybe I am expecting to much, and it is not easy to achieve so many request in easy way. I am using Redis and it has pub sub protocol. So maybe there is some easy way to push data from Redis to JavaScript at the frontend?

private def checkQueue(r: RedisClient, numbers: List[Int]): Unit = {
    val d1 = new Date()
    val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    val now = new Date()
    for (nr <- numbers) {
        val ranges = r.hkeys("user_" + nr + ":soldier:queue_time")
        ranges match {
            case Some(ss) => for (range <- ss) {
                val added_time = r.hget("user_" + nr + ":soldier:queue_time", range)
                val saved = format.parse(added_time.get)
                val diff = (now.getTime - saved.getTime) / 1000 // diff in sec
                val interval = r.hget("user_" + nr + ":soldier:interval", range)
                val modulo = diff % interval.get.toInt
                if (modulo == 0) {
                    val queue_amount = r.hget("user_" + nr + ":soldier:queue_amount", range)
                    if (queue_amount.get.toInt >= 1) {
                        r.hincrby("user_" + nr + ":soldier:amount", range, 1)
                        r.hincrby("user_" + nr + ":soldier:queue_amount", range, -1)
                    }
                }
            }
            case None =>
        }
    }
    val d2 = new Date()
    println("loop time: " + (d2.getTime - d1.getTime) + " milliseconds")
}
So maybe there is some easy way to push data from Redis to JavaScript at the frontend?

Check Webdis it provides HTTP interface to Redis. Including Pubsub feature:

Pub/Sub using Transfer-Encoding: chunked, works with JSONP as well. Webdis can be used as a Comet server.

You can also use Play to write your own Comet , Stream , or Websocket server. You can also check this project for inspiration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM