0
0
NestJSframework~3 mins

Why Subscriptions in NestJS? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could magically update itself the moment something changes?

The Scenario

Imagine building a chat app where users want to see new messages instantly without refreshing the page.

You try to manually check the server every few seconds to get new messages.

The Problem

Manually asking the server repeatedly wastes resources and slows down the app.

It also causes delays and can miss updates between checks.

This makes the app feel slow and clunky.

The Solution

Subscriptions let the server push new data to the client instantly when something changes.

This means users see updates live without wasting resources on constant checks.

Before vs After
Before
setInterval(() => fetch('/messages'), 3000);
After
const subscription = pubSub.asyncIterator('newMessage');
What It Enables

Subscriptions enable real-time, efficient, and smooth data updates between server and client.

Real Life Example

In a live sports app, fans get instant score updates as goals happen without refreshing.

Key Takeaways

Manual polling is slow and resource-heavy.

Subscriptions push updates instantly from server to client.

This creates fast, real-time user experiences.