It sounds like you already know the basic difference between ask
and tell
, but don't understand how tell
could be used to involve other actors in handling HTTP requests.
In order for it to make sense to use tell
in your HTTP request handlers, you have to use an HTTP server that does not require that request handlers return their responses. Spray is such an HTTP server.
In Spray a request handler does not return its response; it is given a RequestContext object, and responding to the request involves invoking some method on it. You can simply send that RequestContext to another actor, which can then respond to the request:
path("foo") {
rc => rc complete "foo" // respond here
} ~
path("bar") {
rc => barActor ! DoBar(rc) // send it to bar; NO RESPONSE HERE
}
Then the actor referred to by barActor could say
case DoBar(rc) =>
rc complete "bar" // respond here, in another actor
The fact that Spray packages up the request context into an object that can be passed around and completed from any actor is a great fit for the actor model. If, on the other hand, your web framework requires that the invoked handler return the response, then if you want to involve another actor your only choice is to use ask
.
Typesafe announced that Play
will soon use Spray underneath. I hope that means that Play
will then allow requests to be sent along to other actors for processing.