mashupbots / socko

A Scala web server powered by Netty networking and AKKA processing.
Other
255 stars 51 forks source link

api change design question: passing webServer.webSocketConnections to actors #75

Closed simbo1905 closed 10 years ago

simbo1905 commented 10 years ago

With the 0.4.0 api changes what is the canonical way to pass webServer.webSocketConnections to worker actors? With 0.3.0 I was passing the jboss channel from the handshake with a 'register' message to my workers which would write out to the sockets. Now with 0.4.0 I am sending the webServer.webSocketConnections to the main worker actor to initialize it. Take a look at line 147 in this https://github.com/simbo1905/planning-poker/blob/v0.2/src/main/scala/scrumpoker/server/ScrumPokerApp.scala which leads to me making the main worker a state machine which is awaiting that message so that it can start to work which is line 49 here https://github.com/simbo1905/planning-poker/blob/v0.2/src/main/scala/scrumpoker/game/ScrumEngine.scala This seems clumsy. What's the expected way to have workers get a handle on the client websockets to send messages down?

An approach I am thinking is to ask the workers for their response from inside the Routes and use a future to send the response into the webSocketConnections without having to export it to the workers. That would seem like better encapsulation at the expense of increased message passing.

Thanks for your consideration in this matter.

simbo1905 commented 10 years ago

p.s. if you were to create a socko tag on stackoverflow i would ask questions there rather than raise issues ;-)

simbo1905 commented 10 years ago

I tried an approach of using an ask in the routes to get a response from business logic actors. This has the problem that you have to define the routes first to pass them into the constructor of the webserver. As the webserver does not yet exist you cannot use webserver.webSocketConnections in the routes. You get compile errors such as "forward reference extends over definition of value routes". I am back to thinking that its hard to use the new 0.4 api with business logic written as actors as I cannot find a way to conveniently lookup webserver.webSocketConnections inside the routes.

simbo1905 commented 10 years ago

I have solved this with using a var to hold the webServer which can be seen at line 64 here:

https://github.com/simbo1905/planning-poker/blob/v0.6/src/main/scala/scrumpoker/server/ScrumPokerApp.scala

This allows the routes to use the var at lines 99 and 117 to push responses from asking the worker actors.

Can the api be enhanced so that there is a way in the routes to lookup the webserver to interact with it so that users of your api dont have to use a var to do the obvious of working with the webserver.webSocketConnections from within the routes?

veebs commented 10 years ago

Been on holidays but back now.

If you don't want to use a var you can do what I've done in the ChatApp example:

https://github.com/mashupbots/socko/blob/master/socko-examples/src/main/scala/org/mashupbots/socko/examples/websocket/ChatApp.scala

An alternative is to start up a wrapper actor in main() with webServer as an argument. You can then send messages to the actor which will use webServer to send messages.

class MyActor(webServer: WebServer) extends Actor {
  val log = Logging(context.system, this)
  def receive = {
    case s: String ⇒ webServer.webSocketConnections.writeText(s)
    case _      ⇒ log.info("received unknown message")
  }
}
veebs commented 10 years ago

BTW - I've also setup a socko google group. I've not had time to publisize it yet - but please feel free to post to it an ask questions if you prefer.

simbo1905 commented 10 years ago

I prefer stackoverflow to mail distro groups as it has a richer set of features for peer reviewing answers and the content is in the public domain and popular answers get translated into foreign languages etc. I don't have enough rep to create "socko" tag on stackoverflow yet.

simbo1905 commented 10 years ago

I posted a better version of this question which veebs answered and which I implemented over at https://github.com/mashupbots/socko/issues/76

simbo1905 commented 10 years ago

The socko googlegroup is members only and i cannot browse it without asking to join it. Is there something restricted about socko and the way that it is discussed outside of the public domain?

veebs commented 10 years ago

I've allowed the public to join. I think you just need to sign in with your google id, join and you should be able to post.