0
0
NestJSframework~10 mins

Subscriptions in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subscriptions
Client sends subscription request
Server sets up subscription resolver
Server listens for events/data changes
Event occurs -> Server pushes update
Client receives real-time data
Client can unsubscribe anytime
Subscription ends
This flow shows how a client subscribes to real-time data updates, the server listens and pushes updates, and the client receives them until unsubscribing.
Execution Sample
NestJS
import { Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'graphql-subscriptions';

const pubSub = new PubSub();

@Resolver()
export class MessageResolver {
  @Subscription(() => String)
  messageSent() {
    return pubSub.asyncIterator('messageSent');
  }
}
This code sets up a GraphQL subscription in NestJS that listens for 'messageSent' events and pushes string messages to subscribed clients.
Execution Table
StepActionServer StateClient StateOutput
1Client sends subscription requestWaiting for subscriptionRequest sentNo data yet
2Server sets up subscription resolverSubscription resolver readyWaiting for dataNo data yet
3Server listens for 'messageSent' eventsListening for eventsSubscribedNo data yet
4Event 'messageSent' triggered with 'Hello!'Event receivedReceives update'Hello!' sent to client
5Client receives 'Hello!'Idle, waiting for next eventData updated: 'Hello!'Client UI updates
6Client unsubscribesSubscription closedUnsubscribedNo further updates
7No more events sentNo active subscriptionsNo active subscriptionsSubscription ended
💡 Client unsubscribes or connection closes, ending the subscription.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
subscriptionActivefalsetruetruefalsefalse
lastMessagenullnull'Hello!''Hello!''Hello!'
Key Moments - 3 Insights
Why does the client not receive any data immediately after subscribing?
Because the subscription only pushes data when an event occurs (see Step 3 and 4 in execution_table). Until then, the client waits.
What happens if the client unsubscribes but the server still emits events?
The client will not receive updates after unsubscribing (Step 6), as the subscription is closed on the server side.
How does the server know which events to send to which clients?
The server uses the subscription resolver and PubSub asyncIterator to listen for specific events and push updates only to subscribed clients (Step 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the client first receive data?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Check the 'Output' column for when 'Hello!' is sent to client.
According to variable_tracker, what is the value of 'subscriptionActive' after the client unsubscribes?
Afalse
Bnull
Ctrue
D'Hello!'
💡 Hint
Look at the 'After Step 6' column for 'subscriptionActive'.
If the server never triggers the 'messageSent' event, what will the client see?
AError message
BContinuous data updates
CNo data received
DSubscription ends immediately
💡 Hint
Refer to Steps 3 and 4 in execution_table where data is sent only on event.
Concept Snapshot
Subscriptions in NestJS GraphQL:
- Use @Subscription decorator in resolver
- Return pubSub.asyncIterator(eventName) to listen
- Client subscribes and waits for events
- Server pushes updates on event
- Client receives real-time data until unsubscribing
Full Transcript
Subscriptions in NestJS allow clients to receive real-time updates from the server. The client sends a subscription request, and the server sets up a subscription resolver that listens for specific events using PubSub. When an event occurs, the server pushes data to all subscribed clients. The client receives these updates and can unsubscribe anytime to stop receiving data. This flow enables live data communication, such as chat messages or notifications, in a simple and efficient way.