For reference, this question has roots from Scala method performance (collect or foreach or other) looping through sockets?
I’m storing a reference to a websocket inside an actor, and then subscribing that actor to an Akka EventStream:
val socketActor = system.actorOf(Props(new Actor {
val socket = WebSocketConnection
def receive = {
case d: AppMessage ⇒ socket.send(d)
}
}))
system.eventStream.subscribe(socketActor, classOf[AppMessage])
What bugs me is that the only classifier I can make with an EventStream is a class type. So if you want to route messages to different actors, say based on userId, do you need to create multiple EventStreams and manually build an EventBus or is there something here that I am missing?
It would be nice if I could do something simply like:
system.eventStream.subscribe(socketActor, Map("userId" -> userId, "teamId" -> teamId) )
This may be simply a conceptual issue, as I’m not quite sure what EventStream represents.
This was my solution based on the
ActorEventBusbased on this Gist: https://gist.github.com/3757237I found this to be a lot more maintainable than dealing with EventStreams. Perhaps multiple EventStreams will be needed in the future, but at this point it readily supports current infrastructure.
MessageBus
First, the MessageBus that handles the outgoing messages to sockets wrapped in actors, based on PubSub channels:
Socket Access with Actors
Here’s the actor that actually contains the socket: