
GraphQL Over WebSockets: Real-Time APIs
A deep architectural dive into GraphQL Subscriptions, exploring the modern graphql-ws subprotocol, connection lifecycles, stateful connection scaling, and production resilience.
Modern web applications တွေမှာ dynamic dashboards, collaborative canvas editing, live order tracking, chat engines နဲ့ financial market tickers တွေလို real-time interactive user experiences တွေ မဖြစ်မနေ လိုအပ်လာပါတယ်။ Standard GraphQL architecture ဟာ HTTP POST request-response cycle ပေါ်မှာ အခြေခံထားပြီး client က query ဒါမှမဟုတ် mutation request ပို့လိုက်တိုင်း server က single JSON response ပြန်ပေးတဲ့ stateless model ဖြစ်ပါတယ်။ ဒါပေမဲ့ backend state ပြောင်းလဲသွားတိုင်း instant push notifications ရရှိဖို့အတွက် client က short polling တွေ အဆက်မပြတ် လုပ်နေမယ်ဆိုရင် network bandwidth overhead, high latency နဲ့ unneeded database load တွေ အကြီးအကျယ် ဖြစ်ပေါ်စေပါတယ်။
ဒီပြဿနာကို ဖြေရှင်းဖို့အတွက် GraphQL specification ထဲမှာ Subscription operation type ကို သတ်မှတ်ပေးထားပြီး underlying transport layer အဖြစ် persistent full-duplex TCP socket ဖြစ်တဲ့ WebSockets ကို အသုံးပြုကြပါတယ်။ ဒီ engineering insight မှာ GraphQL over WebSockets ရဲ့ core architecture, modern graphql-ws subprotocol wire format, connection handshake lifecycle, distributed horizontal scaling strategies နဲ့ production-grade resilience patterns တွေကို စနစ်တကျ လေ့လာဆန်းစစ်သွားပါမယ်။
Beyond Request-Response: The Real-Time Imperative
Standard GraphQL queries နဲ့ mutations တွေဟာ client-initiated pull model အနေနဲ့ ကောင်းမွန်စွာ အလုပ်လုပ်ပါတယ်။ Client က လိုချင်တဲ့ field schema ကို declare လုပ်ပြီး server က exact match response ပြန်ပေးပါတယ်။ ဒါပေမဲ့ collaborative canvas, live dashboards နဲ့ multi-user status sync တွေမှာ server-side state ဟာ dynamic ဖြစ်စွာ ပြောင်းလဲနေနိုင်ပါတယ်။
HTTP request-response cycle အဟောင်းမှာ server က client ဆီ unsolicited data ပို့ခွင့် မရှိပါဘူး။ Client က continuously poll လုပ်မယ်ဆိုရင် 99% သော requests တွေဟာ unchanged data တွေကိုသာ ရရှိပြီး server thread pools တွေကို consume ဖြစ်စေပါတယ်။ Push-based communication model တည်ဆောက်နိုင်မှသာ database event trigger ဖြစ်တာနဲ့ client UI components တွေဆီ sub-millisecond latency ဖြင့် reactive updates တွေ ချက်ချင်းရောက်ရှိစေမှာ ဖြစ်ပါတယ်။
HTTP Overhead and the Persistent Socket Paradigm
HTTP polling မှာ request တိုင်းအတွက် TCP handshake, TLS negotiation, repetitive HTTP headers (cookies, auth tokens) တွေ ပါဝင်တာကြောင့် packet overhead မြင့်မားပါတယ်။ WebSockets ကတော့ standard HTTP upgrade handshake (RFC 6455) ဖြင့် single TCP connection ကို full-duplex persistent stream အဖြစ် switch လုပ်ပေးပါတယ်။
Handshake အောင်မြင်ပြီးချိန်မှာ HTTP headers မပါတော့ဘဲ minimal framing overhead (2 to 10 bytes per frame) ဖြင့် bidirectional communication ပြုလုပ်နိုင်ပါတယ်။ Client နဲ့ Server အကြား socket အမြဲ open ဖြစ်နေတာကြောင့် server events ready ဖြစ်တာနဲ့ zero-latency နီးပါးဖြင့် client ဆီ data frames တွေကို push ပေးနိုင်ပါတယ်။ GraphQL Subscriptions တွေဟာ ဒီ persistent socket ပေါ်မှာ operation payload တွေကို stream အနေနဲ့ emit လုပ်ပေးတာ ဖြစ်ပါတယ်။
The Modern graphql-ws Subprotocol Architecture
GraphQL community ထဲမှာ legacy protocol တစ်ခုဖြစ်တဲ့ subscriptions-transport-ws (Apollo မှ စတင်ခဲ့သော protocol) ကို နှစ်ပေါင်းများစွာ သုံးခဲ့ကြပေမဲ့ လက်ရှိ modern ecosystem မှာတော့ deprecate ဖြစ်သွားခဲ့ပြီး official GraphQL over WebSocket Protocol specification အသစ်ဖြစ်တဲ့ graphql-ws (graphql-transport-ws subprotocol) ကို standard အဖြစ် ပြောင်းလဲအသုံးပြုနေကြပါပြီ။
graphql-ws protocol ဟာ strict state machine တစ်ခုအနေနဲ့ အလုပ်လုပ်ပြီး client နဲ့ server အကြား JSON message frame formats တွေကို standardise ပြုလုပ်ပေးထားပါတယ်:
connection_init: Socket open ဖြစ်ပြီးချင်း client က auth token အပါအဝင် connection parameters ပေးပို့ပါတယ်။connection_ack: Server က credentials validate လုပ်ပြီး connection လက်ခံကြောင်း confirm ပြုလုပ်ပါတယ်။subscribe: Client က unique subscriptionidနဲ့အတူ GraphQL document AST ကို ပို့လွှတ်ပါတယ်။next: Server က target topic မှ event trigger တိုင်း data payload ကို client ဆီ push ပေးပါတယ်။complete: Stream ပြီးဆုံးချိန် သို့မဟုတ် client unsubscribe လုပ်လိုတဲ့အခါ stream ကို terminate လုပ်ပါတယ်။ping/pong: Connection alive ဖြစ်နေကြောင်း စစ်ဆေးတဲ့ keep-alive heartbeats ဖြစ်ပါတယ်။
Subscription Handshake and Frame Lifecycle
Client က WebSocket server ဆီ connection စတင်ချိတ်ဆက်တဲ့ အချိန်ကနေ subscription stream အလုပ်လုပ်ပုံ sequence flow ကို အောက်ပါ architectural diagram မှာ မြင်တွေ့နိုင်ပါတယ်:
Client ဘက်က subscription cancel လုပ်လိုတဲ့အခါ complete frame ပေးပို့ပြီး server ဘက်က resource cleanup နဲ့ listener unregistration တွေကို instant memory leak မဖြစ်အောင် ဆောင်ရွက်ပေးပါတယ်။
Production Server Setup with graphql-ws
Production-grade Node.js backend environment တစ်ခုမှာ fastify ဒါမှမဟုတ် standard ws library နဲ့အတူ graphql-ws/lib/use/ws adapter ကို အသုံးပြုပြီး WebSocket server တည်ဆောက်နိုင်ပါတယ်။ Client auth context ကို initial handshake မှာ validate လုပ်ပုံ sample implementation ကို အောက်မှာ လေ့လာနိုင်ပါတယ်:
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import { schema } from './schema';
import { verifyJwt } from './auth';
const wss = new WebSocketServer({ port: 4000, path: '/graphql' });
useServer(
{
schema,
onConnect: async (ctx) => {
const authHeader = ctx.connectionParams?.authorization as string;
if (!authHeader) throw new Error('Unauthorized WebSocket Connection');
const user = await verifyJwt(authHeader.replace('Bearer ', ''));
ctx.extra = { user };
return true;
},
onDisconnect: (_ctx, code, reason) => {
console.log(`Socket disconnected: Code ${code}, Reason: ${reason}`);
},
},
wss
);
ဒီ setup မှာ connectionParams မှ bearer token ကို evaluate လုပ်ပြီး invalid tokens ဖြစ်ပါက connection handshake ကို ချက်ချင်း reject ပြုလုပ်နိုင်တာကြောင့် unauthorized subscription execution တွေကို root level မှာ secure ဖြစ်စေပါတယ်။
Horizontal Scaling with Distributed Pub/Sub Buses
Single server instance မှာ WebSocket subscriptions run ရတာ ရိုးရှင်းပေမဲ့ production cloud architecture မှာ auto-scaling container cluster (Kubernetes, AWS ECS) တွေ သုံးတဲ့အခါ stateful connection management ပြဿနာ စတင်ကြုံတွေ့ရပါတယ်။
WebSocket connections တွေဟာ stateful TCP connections ဖြစ်ပြီး သတ်မှတ်ထားတဲ့ server instance မှာသာ pinned ဖြစ်နေပါတယ်။ ဥပမာ - User A က Server 1 ဆီ ချိတ်ဆက်ထားပြီး User B က Server 2 မှာ order checkout mutation run လိုက်တဲ့အခါ Server 1 က ဒီ event ကို direct မသိနိုင်ပါဘူး။
ဒီ distributed disconnect ကို ဖြေရှင်းဖို့ centralized distributed message broker (Redis Pub/Sub, RabbitMQ, NATS) ကို backend message bus အဖြစ် ချိတ်ဆက်ရပါတယ်။ Mutation run လိုက်တဲ့ server က event ကို Redis topic ဆီ publish လုပ်ပြီး cluster ထဲက websocket servers အားလုံးက subscribe လုပ်ထားတာကြောင့် active client sockets တွေဆီ instant fan-out push ပေးနိုင်မှာ ဖြစ်ပါတယ်။
Connection Resilience, Heartbeats, and Security
Production WebSocket infrastructure မှာ continuous reliability ရရှိစေဖို့ operational concerns တွေကို သေချာ handle လုပ်ထားရပါမယ်:
- Dead Connection Detection (Half-Open Sockets): Mobile clients တွေ network handover ဖြစ်ချိန်မှာ TCP FIN packet မပို့ဘဲ disconnected ဖြစ်တတ်ပါတယ်။ ဒါကို သိရှိဖို့
graphql-wsရဲ့ ping/pong heartbeats ကို 20-30 seconds intervals ဖြင့် configure လုပ်ထားရပါမယ်။ - Token Expiration on Long-Lived Sockets: Long-lived socket connection ဖွင့်ထားစဉ် JWT token expire ဖြစ်သွားတဲ့အခါ socket connection ကို server ဘက်က force close လုပ်ပြီး client ဆီ re-authentication trigger ပေးရပါမယ်။
- Subscription Query Complexity & Rate Limiting: Malicious clients တွေ deeply nested queries တွေ run ပြီး memory exhausting မဖြစ်စေဖို့ query depth analyzers နဲ့ concurrent limits တွေကို enforce လုပ်ထားရပါမယ်။
Transport Trade-Offs: WebSockets vs SSE vs Polling
Real-time architectures ရွေးချယ်ရာမှာ team requirements နဲ့ infrastructure complexity ပေါ် မူတည်ပြီး trade-offs တွေကို နားလည်ထားဖို့ အရေးကြီးပါတယ်:
| Protocol Option | Connection Mode | Directionality | HTTP Infrastructure | Operational Complexity | Ideal Use Case |
|---|---|---|---|---|---|
| Polling | Ephemeral HTTP | Unidirectional (Pull) | Native (Universal proxy support) | Low | Low-frequency background updates |
| Server-Sent Events (SSE) | Persistent HTTP Stream | Unidirectional (Push) | Excellent (HTTP/2 multiplexing) | Medium | Read-only live feeds, notifications |
| GraphQL over WebSockets | Persistent Full-Duplex TCP | Bidirectional (Duplex) | Complex (Sticky sessions, timeouts) | High | Collaborative tools, live chat, dashboards |
Final Thoughts
GraphQL Subscriptions over WebSockets ဟာ modern web applications တွေအတွက် reactive real-time data streaming capabilities တွေကို ပေးစွမ်းနိုင်တဲ့ powerful architecture ဖြစ်ပါတယ်။ Modern graphql-ws subprotocol ကို အသုံးပြုခြင်းအားဖြင့် standardized connection handshake, clean lifecycle management နဲ့ robust heartbeat ping-pong mechanisms တွေကို ရရှိစေပါတယ်။
ဒါပေမဲ့ WebSockets ဟာ stateful persistent TCP connections ဖြစ်တာကြောင့် distributed clusters တွေမှာ Redis Pub/Sub message broker လိုအပ်ပြီး memory footprint နဲ့ connection pool ကို သတိထား design လုပ်ရပါမယ်။ Unidirectional streaming သာ လိုအပ်တဲ့ use cases တွေအတွက် SSE (graphql-sse) ကို သုံးနိုင်ပြီး၊ bi-directional interactive communication နဲ့ complex multi-subscription systems တွေအတွက်တော့ GraphQL over WebSockets ဟာ industry standard enterprise solution ဖြစ်ပါတယ်။