GraphQL Subscription (WebSocket)
This page explains how to connect to the Rewards GraphQL API WebSocket for real-time updates
The Rewards GraphQL API provides a single WebSocket subscription (StatusUpdate) that delivers real-time notifications for:
- XP and level changes
- Virtual currency balance updates
- Wheel token grants and usage
- Challenge progress and completion
- Bonus awards
All updates are user-scoped and require authentication.
Connection Setup
Endpoint
Protocol
The WebSocket uses the graphql-ws protocol (also known as graphql-transport-ws). This is the modern GraphQL over WebSocket protocol, NOT the legacy subscriptions-transport-ws protocol.
When establishing the connection, the client must:
- Upgrade the HTTP connection to WebSocket
- Send a connection_init message with authentication parameters
- Wait for connection_ack before subscribing
Authentication
Authentication happens during the WebSocket handshake via the connectionParams (sent in the connection_init message).
Required parameters:
| Parameter | Type | Description |
|---|---|---|
connection_id | string | Required. A unique identifier for this connection (e.g., UUID). Used to track multiple connections per user. |
Authorization | string | Recommended. JWT Bearer token (e.g., Bearer eyJhbGci...). |
You must provide Authorization. The Authorization header with a JWT is the preferred method.
JWT Token Requirements:
- Must be a valid JWT signed with the configured secret (RSA or HMAC)
- Must contain user identity claims
- Token expiration is validated
Connection Parameters
Subscribing to Updates
Once connected and authenticated, subscribe to the StatusUpdate subscription:
GraphQL Subscription:
Response Format:
| Field | Type | Description |
|---|---|---|
Type | string | The type of update (see Event Types) |
Timestamp | string | ISO 8601 timestamp when the update occurred |
UserID | int | The user ID this update belongs to |
Data | string | JSON-encoded payload with update details |
Handling Disconnects
WebSocket connections can be interrupted for various reasons:
- Network issues
- Server restarts
- Token expiration
- Idle timeout
Recommended Reconnection Strategy
Implement exponential backoff with jitter:
Connection State Management
Track connection states and handle transitions:
- Connecting - Initial connection attempt
- Connected - Received connection_ack
- Subscribed - Subscription active, receiving events
- Disconnected - Connection lost, should attempt reconnect
- Error - Authentication failed or unrecoverable error