Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka IO.TCP with Json

I've written a client and server using akka.io.tcp and I'm having a problem when reading the messages sent by the client. I've used json to send the messages. In the client side, I write the message this way:

connection ! Write(ByteString(msgString))

In the server side I have the following:

override def receive: Receive = {
  case Received(data) => listener ! Json.parse(data.utf8String)
  case PeerClosed => {
    context stop self
  }
}

The problem is akka is reading more than one message at a time, so I get an invalid Json. Is there a way to make akka read just one message at a time?

like image 711
Augusto Avatar asked Apr 07 '14 14:04

Augusto


1 Answers

Akka's TCP module is (and is designed to be) very "low level", so we're not providing any kind of frame delimiters. You should treat it more like an TCP level building block you then have to build your stuff on.

In your example, the Actor would have to aggregate the incoming chunks of data, and detect when a json document is "complete", then trigger the unmarshalling.

The idea of providing something out of the box for this is quite compelling... I'll ask the guys what our plans are for supporting these kinds of use cases.

Still, I hope this helped!

like image 127
Konrad 'ktoso' Malawski Avatar answered Sep 16 '22 14:09

Konrad 'ktoso' Malawski