0
0
GraphQLquery~3 mins

Why WebSocket transport in GraphQL? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could get updates the moment they happen, without asking again and again?

The Scenario

Imagine you want to get live updates from a server, like new chat messages or stock prices, but you have to keep asking the server again and again if there is anything new.

The Problem

Manually asking the server repeatedly is slow and wastes resources. It can miss updates between checks and makes your app feel laggy and unresponsive.

The Solution

WebSocket transport keeps a constant connection open between your app and the server. This way, the server can instantly send updates as they happen, making your app fast and smooth.

Before vs After
Before
while(true) { fetch('/updates'); await new Promise(resolve => setTimeout(resolve, 5000)); }
After
const ws = new WebSocket('wss://server'); ws.onmessage = (msg) => handleUpdate(msg);
What It Enables

It enables real-time, instant communication between client and server without delays or wasted effort.

Real Life Example

Live sports scores updating on your phone as the game happens, without you refreshing the page.

Key Takeaways

Manual polling is slow and inefficient.

WebSocket transport keeps a live connection open.

This allows instant, real-time updates from server to client.