How to handle websockets using callbacks in 2.6

websockets

(Dilip) #1

How to use this feature in 2.6.
I have a requirement where in when i get a websocket connection request, i need to verify the authenticity of the connection by calling service A.

If authenticated successfully, when i receive any message, i need to check for the message format, change it to POJOs and call service B.

I need handlers for onOpen, onClose, onMessage and onError events like below which was supported in 2.5 & below.

Please let me know, how i can achieve this in 2.6. Thank you in advance

https://www.playframework.com/documentation/2.5.x/JavaWebSockets#Handling-WebSockets-using-callbacks


(Marcos Pereira) #2

Hey @dilip489,

WebSockets are now based on Akka Streams, so that you can it to define flows of how the messages will be handled.

For example, you can use acceptOrResult to verify the identity and reject the connection. You can use Akka Streams directly to define a flow that transforms the message as you want, but keep in mind that Play already has a way to map from JSON to POJOs if that is the only transformation you need. And finally, you can use Akka Streams watchTermination to handle disconnection.

I also recommend to take a look at this example here:

Best.