Playground API
DocsStatsBlogStudio
Documentation Tree
Technical BlogFeature Deep Dives
  • Introduction
  • Quickstart
    5 min
  • How It Works
  • Recipes & Cookbooks
  • Platform Comparisons
  • Real-World Showcase
  • Interactive Studio
    Studio
  • GraphiQL IDE
    IDE
  • Session Quotas & Activity
  • Network Chaos Simulator
  • Atomic Sandbox Reset
  • Overview & Models
    Hub
  • Users Resource
  • Posts Resource
  • Comments Resource
  • Todos Resource
  • Custom Collections
    Custom
  • Multipart File Uploads
    Upload
  • Dynamic SVG Avatars
    SVG
  • Image Thumbnails
    CDN
  • Relational Filtering
  • Full-Text Search
  • Dynamic Sorting
  • Offset Pagination
  • Cursor Pagination
    Scroll
  • CSV & Excel Export & Import
    IO
  • Custom Collections
    CRUD
  • Overview & Architecture
    Hub
  • JWT Auth Flow
  • Refresh Token Rotation
    Mutex
  • RBAC Permission Matrix
    Roles
  • Expiry Simulation
  • Clock Skew Drift
  • Password Recovery Loop
  • Dual-Mode Sandboxing
  • GraphiQL IDE
    IDE
  • Relational Queries
  • Stateful Mutations
  • Realtime Subscriptions
  • Overview & Flowcharts
    Hub
  • Hosted Checkout
    Stripe
  • Payment Intents API
  • 3DS Challenge Modal
    Modal
  • Customers Vault
  • Charges & Refunds
  • Test Cards Catalog
  • Overview & Channels
    Hub
  • Virtual Email Mailbox
    Mailtrap
  • Virtual SMS Terminal
    Phone
  • In-App Notifications
  • Message Dispatcher
  • Realtime Studio
    Studio
  • Native WebSocket (/ws)
  • Socket.io Gateway
  • Presence & Echo Bot
  • Server-Sent Events (SSE)
    SSE
  • Analytics Telemetry
  • Webhook Subscriptions
  • HMAC SHA-256 Signatures
  • Delivery Logs
  • Manual Retry Simulator
  • Network Latency Delay
  • HTTP Status Codes
  • Rate-Limit Simulator
    429
  • Flaky Network & Jitter
    Chaos
  • Session Quotas & Activity
  • JSON Snapshots
    JSON
  • Headless CI/CD Testing
    CI
  • Mobile QR Code Sync
  • System Metrics & Health
  • Atomic Sandbox Reset
  • Official TypeScript SDK
  • Multi-Language Generators
  • DevTools Extension
  • OpenAPI 3.1 Spec
    JSON
  • Postman Collection v2.1
  • Bruno Collection
  • Insomnia Workspace
  • TypeScript .d.ts
    .d.ts
  • AI Prompt Rules
    Rules
  • Context Index (llms.txt)
  • Full Schema (llms-full.txt)
  • Manifest (product.json)
  • All Feature Articles
    Blog
  • React CRUD Without Backend
    Deep Dive
  • Why Static APIs Fail
  • Mocking Stateful Auth
  • WebSockets & SSE Guide
Technical Blog
Articles

In-depth articles explaining stateful mock APIs, WebSockets, payments, and frontend resilience.

Read Articles
PreviousStateful Mutations
NextOverview & Flowcharts
GraphQL Gateway

Real-Time GraphQL Subscriptions

Listen to live resource mutation events over WebSockets using the modern graphql-ws protocol. Stream new blog posts, discussion comments, and task modifications instantaneously.

GraphQL Split Link & Real-Time Subscriptions Engine
GraphQL Protocol
Playground API GraphQL Split Link and Real-Time Subscriptions Engine Workflow Diagram
Click to expand full architecture
Directional client link: Queries and mutations route via HTTP POST, while subscriptions establish persistent graphql-ws WebSocket streams.Interactive High-Resolution Flowchart
1Client Router

Apollo Split Link

Client tests operation definition to route HTTP (queries) vs WebSocket (subscriptions).

2PubSub Bus

In-Memory PubSub Bus

REST & GraphQL mutations publish typed events (postAdded, commentAdded) to channel topics.

3Live Stream

graphql-ws Streaming

Pushes filtered real-time GraphQL query payloads down to active WebSocket subscribers.

Interactive Subscription Visualizer

Select a subscription channel, connect the live WebSocket socket, or simulate trigger events:

ws://localhost:3001/graphqldisconnected
[12:00:00]systemSubscription simulator initialized. Select a topic and connect to test graphql-ws event streaming.

The graphql-ws Handshake Lifecycle

Playground API adheres to the official W3C subprotocol graphql-transport-ws:

1. Handshake
connection_init

Client connects via WebSocket and transmits credentials in payload.

2. Acknowledgement
connection_ack

Gateway acknowledges the subprotocol and confirms readiness for subscription requests.

3. Registration
subscribe

Client sends subscription query document and assigns a unique client operation ID.

4. Streaming Push
next / complete

Server broadcasts mutation events matching the active channel in full JSON format.

Supported Subscription Topics

Pre-configured subscription channels available on the Playground API GraphQL gateway:

Topic NameFilter ParametersTrigger ActionEmitted Entity
postAddedNone (Global stream)Fires whenever any new post is created via REST or GraphQL mutation.Post
commentAddedpostId / post_id: IDFires when a comment is attached to a post (filterable by target postId).Comment
todoUpdateduserId / user_id: IDFires on checklist task creation, title change, or completion status toggles.Todo
customRecordMutatedcollection: StringStreams real-time updates across dynamic developer custom collections.CustomRecord

Client Integration Recipes

Recommended implementations for full-duplex GraphQL WebSocket subscriptions:

subscriptionLink.ts
1
// Apollo Client Split Link (HTTP + WebSocket)
2
import { ApolloClient, InMemoryCache, split, HttpLink } from '@apollo/client';
3
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
4
import { createClient } from 'graphql-ws';
5
import { getMainDefinition } from '@apollo/client/utilities';
6
7
// 1. HTTP Link for Queries & Mutations
8
const httpLink = new HttpLink({
9
uri: 'https://playground.nileslabs.com/api/v1/graphql',
10
});
11
12
// 2. WebSocket Link for Subscriptions
13
const wsLink = new GraphQLWsLink(
14
createClient({
15
url: 'wss://playground.nileslabs.com/api/v1/graphql',
16
connectionParams: {
17
// Optional session authentication:
18
// token: 'your-session-jwt-or-sandbox-id',
19
},
20
})
21
);
22
23
// 3. Directional Split Link
24
const splitLink = split(
25
({ query }) => {
26
const definition = getMainDefinition(query);
27
return (
28
definition.kind === 'OperationDefinition' &&
29
definition.operation === 'subscription'
30
);
31
},
32
wsLink,
33
httpLink
34
);
35
36
export const client = new ApolloClient({
37
link: splitLink,
38
cache: new InMemoryCache(),
39
});