Skip to content
Tutorials Center
My learning

Topics

Lesson 97 of 133 ยท Frameworks and Ecosystem

Real-Time, Queues and the Wider Ecosystem

Tour the wider Node.js ecosystem - job queues like BullMQ, message brokers, cron scheduling, WebSocket frameworks, GraphQL, gRPC, server-side rendering and Deno and Bun compared.

A web server that answers requests is only the beginning of a real system. Sooner or later you need to send an email without making the user wait, run a nightly cleanup, push live updates to a browser, let services talk to each other, or serve a flexible API to a mobile app. The Node.js ecosystem has well-known answers for each of these, and knowing the vocabulary helps you choose, even before you pick a library.

This lesson is a guided tour. For each topic we explain the problem, show the idea with code that runs in the editor (a hand-written stand-in, because no npm packages are installed here), and then show the real library as a read-only example, clearly labelled. The goal is not to teach every API but to give you a map: what exists, when to use it, and what to watch out for.

Job queues: do slow work later

Imagine a signup form. After the user submits, you want to send a welcome email, resize an avatar and notify your sales system. If the request handler does all of that, the user waits seconds, and if the email server is down, the signup fails. A job queue fixes this: the handler puts a small description of the work (a job) into a queue and answers immediately. Separate workers take jobs from the queue and run them in the background, retrying on failure.

The key properties of a good queue are:

  • Durability: jobs survive a crash or restart (they are stored in Redis or a database, not in memory).
  • Retries with backoff: a failed job is tried again after a growing delay.
  • Concurrency limits: a worker processes at most N jobs at a time.
  • Scheduling: delayed jobs, repeating jobs, priorities.
  • Visibility: you can see waiting, active, failed and completed jobs.

Here is a minimal in-memory queue with retries and a concurrency limit. It is not durable (it vanishes when the process ends), but it shows the mechanics clearly.

Node.js A small job queue with retries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Job 2 fails twice and succeeds on the third attempt; job 3 can never succeed (a bad address), so it gives up after three. A real queue moves exhausted jobs to a dead-letter list so a human can inspect them. Notice the design rule this teaches: jobs must be idempotent, meaning safe to run twice. Retries and crashes mean a job can run more than once, so "send welcome email" should check whether it was already sent.

In production the standard library is BullMQ, which stores jobs in Redis. This is a read-only example because it needs npm and a Redis server:

JavaScript
// Needs: npm install bullmq, and a running Redis server (read-only example)
import { Queue, Worker } from 'bullmq';

const connection = { host: '127.0.0.1', port: 6379 };

// In the web process:
const emailQueue = new Queue('email', { connection });
await emailQueue.add('welcome', { userId: 42 }, {
  attempts: 5,
  backoff: { type: 'exponential', delay: 1000 },
  removeOnComplete: 1000,
});

// In a separate worker process:
new Worker('email', async (job) => {
  await sendWelcomeEmail(job.data.userId);
}, { connection, concurrency: 10 });

Run workers as separate processes (or containers) from the web server so that a heavy job cannot slow requests, and so you can scale them independently. For CPU-heavy jobs also consider worker threads, and for scaling in general see Clustering and Scaling.

Message brokers and publish/subscribe

A queue gives each job to one worker. Sometimes you want to tell many listeners about something that happened: "order 981 was paid" matters to the email service, the analytics service and the warehouse. That is publish/subscribe (pub/sub): publishers send events to a topic, and every subscriber of that topic receives a copy. The component in the middle is a message broker.

Inside one process, EventEmitter is a tiny pub/sub (see Events and EventEmitter). Between processes you need a broker. The common ones:

Broker Good for Notes
Redis (pub/sub, streams) Simple events, caching plus messaging Plain pub/sub drops messages if nobody listens; streams persist
RabbitMQ Task routing, work queues, flexible exchanges Messages are acknowledged; mature and widely used
Apache Kafka High-volume event logs, replay, analytics A durable, ordered log; more operational work
NATS Very fast, lightweight messaging Simple to run
Cloud services SQS/SNS, Google Pub/Sub, Azure Service Bus No servers to manage

Two delivery promises matter. At-most-once means a message may be lost but is never repeated. At-least-once means it is never lost but may be delivered again, so consumers must be idempotent. True exactly-once delivery is very hard in distributed systems; most systems choose at-least-once plus idempotent handlers.

The next example is a small typed event bus built on EventEmitter, showing the pattern: publishers know only the topic name, never who listens.

Node.js Pub/sub with decoupled subscribers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The benefit is loose coupling: adding a fourth reaction means adding a subscriber, not editing payOrder. The cost is that the flow becomes harder to follow ("who reacts to this?") and failures are less visible, so log every consumed event with its ID.

A real broker version, with RabbitMQ and the amqplib package (read-only):

JavaScript
// Needs: npm install amqplib, and a RabbitMQ server (read-only example)
import amqp from 'amqplib';

const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
await channel.assertExchange('events', 'topic', { durable: true });

// Publisher
channel.publish('events', 'order.paid', Buffer.from(JSON.stringify({ id: 981 })));

// Subscriber (its own durable queue, bound to the topic)
const { queue } = await channel.assertQueue('email-service', { durable: true });
await channel.bindQueue(queue, 'events', 'order.*');
await channel.consume(queue, (message) => {
  console.log('got', JSON.parse(message.content.toString()));
  channel.ack(message); // tell the broker the message was handled
});

Scheduled work: cron

Many tasks are tied to the clock: "send the weekly report on Monday at 08:00", "delete expired sessions every hour". The Unix tool cron describes schedules with five fields: minute, hour, day of month, month, day of week.

TEXT
*/15 * * * *     every 15 minutes
0 8 * * 1        every Monday at 08:00
30 2 1 * *       at 02:30 on the first day of each month

In Node.js you can use setInterval for simple periodic work in one process, but it has weaknesses: it forgets everything on restart, and if you run three instances of your app, all three run the job. Use a library such as node-cron or Croner for cron expressions in one process, or let the platform schedule it (Kubernetes CronJob, cloud schedulers, systemd timers, or repeatable jobs in BullMQ, which run once no matter how many workers exist).

Understanding what an expression means is easy to practice. Here is a small matcher for the five fields that supports *, lists, ranges and steps.

Node.js Evaluating cron expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

This toy ignores several real rules (names like MON, how day-of-month and day-of-week combine, time zones), which is exactly why you should use a library. Always run scheduled jobs in a defined time zone (UTC is the safest), and design them so a missed or doubled run is harmless.

WebSockets and real-time frameworks

When a browser needs live updates (chat, dashboards, notifications, multiplayer), HTTP request/response is not enough. WebSockets keep a connection open so either side can send messages at any time; the lesson WebSockets in Node.js shows the protocol, and Server-Sent Events covers the simpler one-way alternative.

Raw WebSockets leave a lot to you: reconnection, rooms, authentication, message acknowledgement, fallbacks for hostile networks, and sharing messages between several server instances. Frameworks add those:

  • ws is the small, fast, low-level library; most others are built on it. Use it when you want full control.
  • Socket.IO adds rooms, namespaces, automatic reconnection, acknowledgements and HTTP long-polling fallback. It has its own protocol, so its client and server must be used together (it is not a plain WebSocket server).
  • uWebSockets.js is a very fast C++ implementation for extreme connection counts.

A read-only Socket.IO example (needs npm):

JavaScript
// Needs: npm install socket.io (read-only example)
import { Server } from 'socket.io';

const io = new Server(3000, { cors: { origin: 'https://example.com' } });

io.on('connection', (socket) => {
  socket.join('lobby');
  socket.on('chat', (text, acknowledge) => {
    io.to('lobby').emit('chat', { from: socket.id, text });
    acknowledge({ ok: true });
  });
});

The big architectural point is scaling real-time servers. Each WebSocket connection is tied to one server process, so a message sent to a room must reach users connected to other instances. The usual fix is a pub/sub backend (Redis or a message broker) that all instances use to relay messages, which is exactly the pattern from the previous section. Also keep in mind that connections are long-lived, so graceful shutdown and rolling deploys need care (see Deploying Node.js Applications).

GraphQL

REST gives each resource its own URL and a fixed response shape. GraphQL gives a client one endpoint and a query language: the client asks for exactly the fields it wants, across related objects, in a single request.

TEXT
query {
  user(id: 1) {
    name
    posts { title }
  }
}

The server describes its data with a schema (types and fields) and implements a resolver function per field. The server calls only the resolvers for fields the query asked for. The idea of "resolve only what was asked" is easy to show without a library:

Node.js The resolver idea behind GraphQL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In real projects you use a library (graphql-js, GraphQL Yoga, Apollo Server, Mercurius for Fastify). Strengths: one request for complex screens, a typed contract, and easy evolution for many different clients. Costs to know about: HTTP caching is harder (everything is a POST to one URL), you must protect against expensive queries (depth and cost limits), and the N+1 problem (a posts resolver per user hitting the database each time) needs a batching tool such as DataLoader. For a simple CRUD API, plain REST (see Building REST APIs) is often the simpler choice.

gRPC

gRPC is a framework for service-to-service calls. You write a contract in Protocol Buffers (a .proto file); tools generate client and server code in many languages; messages travel in a compact binary format over HTTP/2, with support for streaming in both directions. It shines inside a backend where services in different languages call each other often and the performance and strict contracts matter. It is a poor fit for calling directly from a browser without a proxy.

TEXT
// greeter.proto (read-only example)
syntax = "proto3";

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
  rpc Chat (stream ChatMessage) returns (stream ChatMessage);
}

message HelloRequest { string name = 1; }
message HelloReply   { string message = 1; }
message ChatMessage  { string text = 1; }

In Node.js you use @grpc/grpc-js with @grpc/proto-loader, or a modern alternative such as Connect (works over plain HTTP/1.1 and in browsers). The binary encoding ties in with what you learn in Binary Protocols.

Server-side rendering and full-stack tools

If you build web pages, Node.js can also render HTML on the server. The choices range from simple to large:

  • Template engines (EJS, Handlebars, Nunjucks, Eta) fill HTML templates with data. Simple and fast for classic server-rendered sites.
  • Meta-frameworks run your UI components on the server: Next.js, Remix / React Router, Nuxt, SvelteKit, Astro, SolidStart. They handle routing, rendering on the server, sending the page to the browser, and "hydrating" it for interactivity. They also handle bundling, which ties to Building and Bundling.
  • Backend frameworks for APIs: Express (the classic), Fastify (faster, with schema validation built in), Koa, Hono (small, runs on every runtime), NestJS (structured, Angular-inspired, uses TypeScript decorators). See Express and Minimal Frameworks.

Pick a boring, well-supported tool for your team size. A framework's value is mostly in its conventions: where things go, how errors are handled, and how others can read your code (see Project Structure and Architecture).

Beyond Node: Deno, Bun and edge runtimes

Node.js is not the only place JavaScript runs on a server.

Runtime Engine Key ideas
Node.js V8 Largest ecosystem; npm; stable LTS lines; most hosting support
Deno V8 Secure by default (permissions), TypeScript built in, URL imports and npm support, built-in tools
Bun JavaScriptCore Fast startup and installs, bundler, test runner and package manager in one binary, high Node compatibility
Edge runtimes (Cloudflare Workers, Vercel Edge) V8 isolates Run close to users, tiny cold starts; limited APIs, no full fs or net

They share the language and many web-standard APIs (fetch, Request, Response, URL, AbortController, ReadableStream, crypto.subtle), and that is the trend that matters: code written against web standards, such as the Hono framework or Web Streams, moves between runtimes with little change. Node.js has embraced it too (see Web Streams and Node.js vs the Browser).

When should you use something else? Choose Node.js when you need the broadest library support, maximum stability and hosting options, or you rely on native addons. Look at Deno for security-sensitive scripting and tooling, at Bun if install and startup speed dominate your workflow, and at edge runtimes for latency-sensitive, small request handlers. Portable code sometimes needs to check where it runs:

Node.js Detecting the current runtime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Feature detection ('fetch' in globalThis) is more robust than runtime sniffing, so prefer it where you can.

Choosing sensibly

An ecosystem this large invites over-engineering. A few guiding questions:

  • Do you really need it yet? A cron job and a jobs table in your database go a long way before you need Redis and a queue library.
  • Can the platform do it? Managed queues, schedulers and WebSocket services remove operations work.
  • How will it fail? For every new moving part (broker, worker, cache) decide what happens when it is down, slow or sends a message twice.
  • Who will maintain it? Prefer popular, actively maintained packages, check their release history, and keep the number of dependencies small (see package.json in Depth).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main benefit of putting slow work such as sending email into a job queue?

    The handler only enqueues a job, so the user does not wait, and a failed job can be retried without failing the request.
  2. 2. Why should job handlers be idempotent?

    With at-least-once delivery a job may be delivered again. Running it twice must not send two emails or charge twice.
  3. 3. Which pattern lets one event reach many independent consumers?

    In pub/sub every subscriber to a topic receives a copy of each event, while a work queue gives each job to a single worker.
  4. 4. Why does a WebSocket server running on several instances usually need Redis or a broker?

    Each connection lives on one instance. A shared pub/sub backend relays messages between instances so rooms work across all of them.
  5. 5. What does a GraphQL server call for a query that asks only for a user's name?

    The query lists the fields it needs, and the server runs only the resolvers for those fields. This is why clients can avoid over-fetching.

Summary

  • Job queues move slow or unreliable work out of the request: durable storage, retries with backoff, concurrency limits and separate worker processes. Make jobs idempotent.
  • Pub/sub brokers (Redis, RabbitMQ, Kafka, NATS, cloud services) broadcast events to many consumers; expect at-least-once delivery.
  • For scheduled work use a cron library or the platform's scheduler, a fixed time zone, and make sure only one instance runs each job.
  • Real-time frameworks (ws, Socket.IO) add reconnection and rooms; scaling them across instances needs a shared pub/sub layer.
  • GraphQL lets clients choose fields through resolvers; gRPC offers fast, typed service-to-service calls over HTTP/2.
  • Server-side rendering and API frameworks (Next.js, Astro, Express, Fastify, Hono, NestJS) provide structure; choose by team and need.
  • Deno, Bun and edge runtimes share web-standard APIs with Node.js, so standards-based code is portable.

Next: The Built-in Test Runner