mandubian/play-actor-room

room.websocket returning a WebSocket object

Opened this issue · 5 comments

As room.websocket directly returns a WebSocket object, how can you deal with a futureRoom in your controller?

For example I want to keep a Map roomId -> room in an actor. In my controller, I want to retrieve a room according to its id:

def websocket(roomId: String) = {
  val futureRoom = // ask the actor the room of id = roomId
  val futureWebsocket: Future[WebSocket] = futureRoom.map(_.websocket(...))
  ???
}

Should we instead make room.websocket return a tuple (Iteratee, Enumerator), so that in this case we get a Future[(Iteratee, Enumerator)] which can be flattened or used with Websocket.async ?

def websocket(roomId: String) = WebSocket.async {
  val futureRoom = // ask the actor the room of id = roomId
  futureRoom.map(_.websocket(...))
}

I see...
Actually I wanted to hide completely iteratee/enumerator since I think people shouldn't care about it... too low level and complex for most users...

What about providing a Room.async to hide it?
def websocket(roomId: String) = Room.async {
val futureRoom = // ask the actor the room of id = roomId
futureRoom.map(_.websocket(...))
}
it would hide the flattening of enumerator/iteratee...
Not so nice but still keeping iteratee/enumerator out of sight...

WDYT?

What type would return room.websocket in that case? If it returns (Iteratee, Enumerator), Room.async will do the same job as WebSocket.async, right?

No I'd like to keep WebSocket as the return type...
Room.async will flatten a Future[WebSocket] into WebSocket as Iteratee.flatten...
Room.flattenWebSocket if you prefer...

Something like:

object Room {

  def async[A]( fws: => Future[WebSocket[A]] )(implicit frameFormatter: WebSocket.FrameFormatter[A]) = {
    WebSocket[A](h => (e, i) => {
      fws onSuccess { case ws => ws.f(h)(e, i) }
    })
  }

}

Oh ok I see, I didn't realize it was possible to do that.
So yes, a Room.async like you suggested would be very useful ;)

I wondered too but it is possible :D
I should try to add that!