Using TCP with AKKA Typed


I’m trying to create a TCP server with AKKA typed. I already got a POC working with AKKA Classic based actors, but I’m struggling with converting the full POC to AKKA typed.

The current roadblock is that AKKA typed does not contain the getSender() method like in classic. To work around this, the original sender must be included inside the message itself. This is not a problem for user defined messages, but it is for messages defined inside AKKA like Tcp.Connected, etc …

To get the TCP server to work. The server needs to respond with a Tcp.Register message to a Tcp.Connected message. But the Tcp.Connected message does not contain an actor reference property to original sender.

Is it possible to create an AKKA TCP server with AKKA typed?

Thanks in advance.

Kind Regards
Hans Habraken

From my experience last year creating a TCP protocol with Akka, I don’t believe this is supported with Typed actors. The Akka System that Akka TCP uses is classic, not typed. That said, you might be able to convert the Akka System to typed. IIRC there is a conversion function to do that on the Akka System (classic). You should also wait for Lightbend to give you the real answer; this is just my vague recollection.

I think it is possible with a classic adapter inbetween, but it is probably easier and cleaner to use the streaming TCP APIs and interact with those from typed actors.

I don’t know of any good samples, but something like each connection stream from the Tcp().bind spawning an actor and using the typed ActorFlow.ask handing off incoming requests and passing responses back (for an easy request-response protocol, protocols that has detached in and out will need some more stream operator acrobatics for example using Flow.fromSinkAndSource).

For a protocol with only strict messages (no need to stream individual protocol messages to the actor) it probably makes most sense to do protocol framing and parsing in the stream so that you get a clear protocol with the actor rather than passing ByteStrings back and forth.

Note that for actor-per-connection you need to start the actor from inside the stream of incoming connections, so using ctx.spawn will not be possible/safe, you can use SpawnProtocol or your own corresponding way of starting an actor with a command for such things.

Relevant doc links: