Skip to content

Instantly share code, notes, and snippets.

@hussachai
Last active February 1, 2016 03:30
Show Gist options
  • Save hussachai/ebacfb5276b414336ab5 to your computer and use it in GitHub Desktop.
Save hussachai/ebacfb5276b414336ab5 to your computer and use it in GitHub Desktop.
class Application extends Controller {
...
implicit val akkaSystem = akka.actor.ActorSystem()
val redis = RedisClient()
def index = Action { implicit request =>
Ok(views.html.pubsub())
}
def subscribe = WebSocket.tryAcceptWithActor[String, String] { request =>
def props(channel: String)(out: ActorRef) = Props(classOf[SubscribeActor], redis,
out, Seq(channel), Nil).withDispatcher("rediscala-worker-dispatcher")
Future.successful(request.getQueryString("channel") match {
case None => Left(Forbidden)
case Some(channel) => Right(props(channel))
})
}
def publish(channel: String) = Action.async { implicit request =>
request.body.asFormUrlEncoded.flatMap{ params =>
params.get("message").map{ message =>
redis.publish(channel, message.head).map{ n =>
if(n > 0) {
log.debug(s"Number of subscriber: $n s")
Ok(n.toString)
}else {
log.debug("No recipient")
Gone
}
}
}
}.getOrElse(Future.successful(BadRequest("No content received")))
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment