randombits
randombits

Reputation: 48450

Play Framework 2.2.0 [scala] - WebSocket.async vs WebSocket.using[T]

Can't seem to find any documentation on this but seeing some examples using WebSocket.async in actions as their return type and others using WebSocket.using[String].

Is there documentation anywhere as to when to use which? I understand that Websocket.using[String] is indicating that the types of messages coming in and out of this action are of type String. What exactly is the difference then using WebSocket.async? The main concern is when to use which and why.

Upvotes: 3

Views: 507

Answers (1)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

Take a look at their respective signatures:

def using[A](f: RequestHeader => (Iteratee[A, _], Enumerator[A]))(implicit frameFormatter: FrameFormatter[A]): WebSocket[A]
def async[A](f: RequestHeader => Future[(Iteratee[A, _], Enumerator[A])])(implicit frameFormatter: FrameFormatter[A]): WebSocket[A]

A bit too much maybe, let's remove the return types and the implicit parameter lists as they're the same:

def using[A](f: RequestHeader => (Iteratee[A, _], Enumerator[A]))
def async[A](f: RequestHeader => Future[(Iteratee[A, _], Enumerator[A])])

The difference is easier to spot right now. The callback accepted by async returns a Future, whereas with using you can't. async is useful when you're working with asynchronous libraries, e.g. Akka, where sending a message to some actor yields a Future. using should be used with synchronous libraries. I hope it makes sense.

Upvotes: 8

Related Questions