akka-http web-socket Frame support question...

424 views
Skip to first unread message

Muthu Jayakumar

unread,
Dec 22, 2016, 1:15:03 PM12/22/16
to Akka User List
Hello there,

I am new to akk-http on writing a web-socket server side code (have written one using Spray-IO though).  My question is on how I would be able to use the server-side apis to send other kinds of control-frames? By the spec @ https://tools.ietf.org/html/rfc6455#page-36
Close 
Ping 
Pong
Data -- Text -- Binary
The apis have the TextFrame and BinaryFrame part of the Frame covered. I guess, the PingFrame and PongFrame would be useful to have the server-side api to help in the "keep-alive" attribute to the connection(as quoted from the spec: "NOTE: A Ping frame may serve either as a keepalive or as a means to verify that the remote endpoint is still responsive."). The same question on CloseFrame may be useful I guess?

Please advice,
Muthu

On a side note...
On top of the akka documentation @ http://doc.akka.io/docs/akka-http/current/scala/http/websocket-support.html, I found the following article useful in getting Web-Socket to hook-up thru actor(s) - https://markatta.com/codemonkey/blog/2016/04/18/chat-with-akka-http-websockets/
To me the gap is on understanding akka-streams better. Not sure if may be useful for the documentation to give some hints on how 'server-push' can be achieved using some code snippet similar to this blog?

Jan Heise

unread,
Feb 1, 2017, 3:32:12 PM2/1/17
to Akka User List
Hi,

I'd like to add, that I'd also be interested in solving this. I'm currently developing a server-backend that should support ping/pong frames to conform to a specification that specifically references those control frames. I am not controlling the development of the clients so having an upper level protocol for keep-alives instead is not something that I can do. Ignoring the whole thing might be fine in the beginning but eventually, I'll have to implement support for ping/pong control frames.

I'd like to reference the following ticket:

Also, in the source, the necessary opcodes are already mentioned/exist. See:

Is there any more support regarding this already in the source that I have missed?
Any pointers on where I might start adding things myself? Where is the place that handles control frames? 
I guess sending a pong message automatically when a ping arrives could be handled easily if somebody points me in the right direction.
Injecting ping messages and handling the eventually handling missing pong messages might need a bit more thought on behalf of someone better than me. But I would like to give it a shot, too.

Best regards,

Jan

Jan Heise

unread,
Feb 1, 2017, 3:42:19 PM2/1/17
to Akka User List
I have to add that I just found support for automatic pong messages in 

Maybe somebody can give me a hint/help me with injecting control frames?

johannes...@lightbend.com

unread,
Feb 21, 2017, 5:43:52 AM2/21/17
to Akka User List
Hi,

injecting frames manually is not supported. Not supported means that we don't offer an API that would allow users to do that.

That said, there's an internal API that allows to specify a frame handler directly:

 * You can case the `UpgradeToWebsocket` header to `UpgradeToWebsocketLowLevel`, this allows you to specify a frame handler flow: https://github.com/akka/akka-http/blob/5932237a86a432d623fafb1e84eeeff56d7485fe/akka-http-core/src/main/scala/akka/http/impl/engine/ws/UpgradeToWebSocketLowLevel.scala#L16-L16

As you can see `Websocket.stack` is just a stack of stages. If you wanted inject and intercept control frames you could basically copy the implementation of `stack` and put another custom stage between `masking` and `frameHandling`. Obviously, you need to know what you are doing not to disrupt the usual way things work. Just injecting `Ping` frames and listening to `Pong` frames should be quite simple, though.

All of these classes are `private[akka]` or `private[http]` so you need to implement anything inside of an `akka.http` package in your project.

Good luck and have fun ;)
Johannes

Surya Choudhury

unread,
Jan 27, 2020, 2:01:41 PM1/27/20
to Akka User List
Hi, 
   We already have a websocket sever based on akka-http + stream 10.0.10. The same server is leveraged by multiple client. The new version of ws client are requesting to have ping/pong based keepalive enable where as the old version of client are not.

Hence we have to support both version of clients, So we need an approach to atop a ping frame for certain websocket connection based on some request header. 
The problem here is we are working with WebSocketDirective which does provided access to RawFrame. 

Can anyone please suggest an approach to solve this scenario

Thanks 
-Surya 
Reply all
Reply to author
Forward
0 new messages