GraphQLWebSocketClient

class GraphQLWebSocketClient(httpClient: HttpClient, url: String, connectionTimeout: Long)

GraphQL WebSocket client implementing graphql-transport-ws protocol.

Protocol: graphql-transport-ws Spec: https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md

Connection Lifecycle:

  1. connect() - Establish WebSocket + send connection_init

  2. Wait for connection_ack

  3. subscribe() - Send GraphQL subscriptions

  4. Receive next/error/complete messages

  5. close() - Clean shutdown

Thread Safety: All methods are thread-safe and can be called from any coroutine.

Parameters

httpClient

Ktor HTTP client with WebSockets installed

url

WebSocket URL (wss://...)

connectionTimeout

Timeout for connection_ack (milliseconds)

Constructors

Link copied to clipboard
constructor(httpClient: HttpClient, url: String, connectionTimeout: Long)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
suspend fun close()

Close connection gracefully.

Link copied to clipboard
suspend fun connect()

Connect to WebSocket server.

Link copied to clipboard
suspend fun ping()

Send ping message (keep-alive).

Link copied to clipboard
fun subscribe(query: String, variables: Map<String, Any>? = null, operationName: String? = null): Flow<JsonElement>

Subscribe to GraphQL operation.