Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for GraphQL over WebSocket Ping and Pong message types #270

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ default Mono<Void> handleWebSocketCompletion() {
return Mono.empty();
}

/**
* Handle the <a href="https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md#ping">ping message</a>
* for "heartbeat/keepalive" that many GraphQL clients send to keep the session alive.
* @return returns a simple "pong" message
*/
default Mono<Void> handleWebSocketPing() {
return Mono.empty();
}

/**
* Handle the <a href="https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md#pong">ping message</a>
* for "heartbeat/keepalive" that many GraphQL clients send to keep the session alive.
* @return returns a simple "pong" message
*/
default Mono<Void> handleWebSocketPong() {
return Mono.empty();
}

/**
* Provides access to a builder to create a {@link WebGraphQlHandler} instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public Mono<Void> handle(WebSocketSession session) {
}
}
return this.graphQlHandler.handleWebSocketCompletion().thenMany(Flux.empty());
case PING:
return this.graphQlHandler.handleWebSocketPing().thenMany(Flux.just(encode(session, null, MessageType.PONG, null)));
case PONG:
return this.graphQlHandler.handleWebSocketPong().thenMany(Flux.empty());
case CONNECTION_INIT:
if (!connectionInitProcessed.compareAndSet(false, true)) {
return GraphQlStatus.close(session, GraphQlStatus.TOO_MANY_INIT_REQUESTS_STATUS);
Expand Down Expand Up @@ -274,6 +278,8 @@ private enum MessageType {
CONNECTION_INIT("connection_init"),
CONNECTION_ACK("connection_ack"),
SUBSCRIBE("subscribe"),
PING("ping"),
PONG("pong"),
NEXT("next"),
ERROR("error"),
COMPLETE("complete");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
}
this.graphQlHandler.handleWebSocketCompletion().block(Duration.ofSeconds(10));
return;
case PING:
this.graphQlHandler.handleWebSocketPing().block(Duration.ofSeconds(10));
return;
case PONG:
this.graphQlHandler.handleWebSocketPong().block(Duration.ofSeconds(10));
return;
case CONNECTION_INIT:
if (sessionState.setConnectionInitProcessed()) {
GraphQlStatus.closeSession(session, GraphQlStatus.TOO_MANY_INIT_REQUESTS_STATUS);
Expand Down Expand Up @@ -313,6 +319,8 @@ private enum MessageType {
CONNECTION_INIT("connection_init"),
CONNECTION_ACK("connection_ack"),
SUBSCRIBE("subscribe"),
PING("ping"),
PONG("pong"),
NEXT("next"),
ERROR("error"),
COMPLETE("complete");
Expand Down