From bb958cb05efaf62f4c7a60d39568ab4e15497292 Mon Sep 17 00:00:00 2001 From: Suraj Gupta Date: Sat, 10 Aug 2024 19:47:57 -0400 Subject: [PATCH] fix: send graphql-specific ping instead of ws ping frame (#117) ## Description When using the new keepalive functionality, we noticed that the pings are sent as [websocket ping frames][1] rather than as [graphql-transport-ws ping messages][2]. Aside from conforming to the protocol, the sub-protocol ping messages should be used to support a heartbeat mechanism from browser (i.e. wasm) clients, where there isn't a concept of ping / pong frames. Specifically, setting a `KeepAliveSettings::interval` from a browser client does not do anything right now. ## Testing Locally tested to confirm that the correct ping messages are now sent from browser clients (and that a gql-conforming server responds with the appropriate pong message). [1]: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2 [2]: https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md#ping --- CHANGELOG.md | 3 +++ src/next/actor.rs | 2 +- src/next/connection.rs | 4 ++++ src/protocol.rs | 2 ++ 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4818e1..c47077f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,12 +15,15 @@ all APIs might be changed. - Fixed ping responses not following the graphql-transport-ws protocol ([#116](https://github.com/obmarg/graphql-ws-client/pull/116)) +- `graphql-transport-ws` ping messages are now sent, instead of websocket ping + frames, when using the `KeepAliveSettings` ([#117](https://github.com/obmarg/graphql-ws-client/pull/117)) ### Contributors Thanks to the people who contributed to this release: - @vorporeal +- @szgupta ## v0.10.1 - 2024-06-08 diff --git a/src/next/actor.rs b/src/next/actor.rs index b05016a..8fc8807 100644 --- a/src/next/actor.rs +++ b/src/next/actor.rs @@ -96,7 +96,7 @@ impl ConnectionActor { code: Some(code), reason: Some(reason), }), - ConnectionCommand::Ping => Some(Message::Ping), + ConnectionCommand::Ping => Some(Message::graphql_ping()), } } diff --git a/src/next/connection.rs b/src/next/connection.rs index 7ce83ac..d46891d 100644 --- a/src/next/connection.rs +++ b/src/next/connection.rs @@ -59,6 +59,10 @@ impl Message { Self::Text(serde_json::to_string(&crate::protocol::Message::Pong::<()>).unwrap()) } + pub(crate) fn graphql_ping() -> Self { + Self::Text(serde_json::to_string(&crate::protocol::Message::Ping::<()>).unwrap()) + } + pub(crate) fn complete(id: usize) -> Self { Self::Text( serde_json::to_string(&crate::protocol::Message::Complete::<()> { id: id.to_string() }) diff --git a/src/protocol.rs b/src/protocol.rs index 8f7fe9d..4ae5865 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -39,6 +39,8 @@ pub enum Message<'a, Operation> { Subscribe { id: String, payload: &'a Operation }, #[serde(rename = "complete")] Complete { id: String }, + #[serde(rename = "ping")] + Ping, #[serde(rename = "pong")] Pong, }