Websocket API server side Ping/Pong

0

Hi,

Does Websocket API do Ping/Pong on the server side to detect if a client is still connected? I find when the network on the client is switched off, Websocket API Gateway still thinks the connection is open until the idle timeout.

Lu

asked 5 years ago6060 views
7 Answers
1

API Gateway WebSocket API does not perform server-side ping but it does respond with pong for the client side ping.

AWS
answered 5 years ago
  • Still no server-side ping? It would be great if you can implement it and trigger the disconnect route when the server-side ping fails.

0

I do not see any documentation where the RFC-compliant ping/pong mechanism of Websocket is supported. Can you point to this? In my experimentation, there is no "pong" response to a websocket "ping" without explicitly coding the integration back-end to respond. Your post suggests that API Gateway will handle client pings without requiring a user-defined integration handle this.

answered 4 years ago
0

I'm not sure how you experimented it, but I tested with wscat successfully.

wscat -c wss://***.execute-api.***.amazonaws.com/*** --slash
connected (press CTRL+C to quit)
'> /ping
< Received pong
'> ⏎

Edited by: jwaataws on Oct 31, 2020 10:13 AM

AWS
answered 3 years ago
0

I use wscat 4.0.1 to connect to API GW for Websocket but nothing happens (in wscat) when sending "/ping"

~ wscat -c wss://***.execute-api.eu-west-1.amazonaws.com/ --slash
Connected (press CTRL+C to quit)
~ /ping
~

What works:

  • Typing /close closes the connection.
  • Sending a message that does not start with "/" triggers the default route in API GW as expected.

But /ping ... nothing?

Edited by: andreasi77 on Jan 11, 2021 5:02 AM

Edited by: andreasi77 on Jan 11, 2021 7:37 AM

Edited by: andreasi77 on Jan 11, 2021 7:39 AM

answered 3 years ago
0

Answering myself since I (with help from AWS) found the solution.

API Gateway Websocket support clients sending ping control frame that defined in RFC doc. The client can send a ping frame to check the connection without any cost.
While API Gateway doesn't perform periodic server-side pings to connected clients, it does respond with pongs for the client-side pings. This is because that RFC dictates that servers MAY send out periodic pings, but that servers MUST respond with the appropriate control frames if pinged.

The reason for "< Received pong" not being printed when using wscat to send "/ping" to server was caused by a missing flag
"-P, --show-ping-pong print a notification when a ping or pong is received"

So the correct command to use is:
~ wscat -c wss://***.execute-api.eu-west-1.amazonaws.com/ --slash -P

answered 3 years ago
0

Thanks andreasi77!
Just tried it out on the command line with wscat and got a pong reply from the established connection. But still - I wonder how that would be used in my frontend javascript / VueJS app. I'm confused when trying to get an idea on how that would be sent and received. Is this being done in the background by the browser on intervals ( but not visible in the developer console network message panel? - I use the Chrome DevTools to monitor stuff in the console). Any insights is much appreciated!

answered 3 years ago
0

I am stuck at the same point - this is closest I have seen as it seems the browsers implementations of WS do not support ping control frames natively

https://stackoverflow.com/questions/26971026/handling-connection-loss-with-websockets/39765960#39765960

answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions