I am using the Playframework along with Scala. I am trying to consume data from Kafka, process the data, and then push the data via websockets to the client. Unfortunately, I am still relatively a newbie with this technology.
When going over the documentation, they mentioned to create an actor when you want to create websockets. Code below is from the website. web socket
import play.api.libs.json.JsValue
import play.api.mvc._
import play.api.libs.streams._
class Controller4 @Inject() (implicit system: ActorSystem, materializer: Materializer) {
import akka.actor._
class MyWebSocketActor(out: ActorRef) extends Actor {
import play.api.libs.json.JsValue
def receive = {
case msg: JsValue =>
out ! msg
}
// do i include my kafka consumer here???
}
object MyWebSocketActor {
def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}
def socket = WebSocket.accept[JsValue, JsValue] { request =>
ActorFlow.actorRef(out => MyWebSocketActor.props(out))
}
}
My question is, where do I put the Kafka consumer code. Do I place it inside the actor? Is this best practice? The reason I am concerned about putting it the kafka consumer there is that it will block.
Thank you in advance for your help.
Akka Streams + Reactive Kafka actually makes this pretty easy. Just wire a Kafka Source to the WebSocket Source to send Kafka messages to a client via the WebSocket. Here is a complete demo with code: https://www.jamesward.com/2016/05/25/combining-reactive-streams-heroku-kafka-and-play-framework/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With