惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
B
Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
博客园_首页
C
Check Point Blog
博客园 - 【当耐特】
美团技术团队
Last Week in AI
Last Week in AI
A
About on SuperTechFans
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
Martin Fowler
Martin Fowler
J
Java Code Geeks
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
Google Developers Blog
F
Fortinet All Blogs

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building Real-Time Dashboards in Angular with WebSockets ...
Placide · 2026-06-21 · via DEV Community

Most dashboards are built the same way: the user lands on the page, data loads, and then... it sits there. Stale. Until the user hits refresh or you set up an awkward polling interval that hammers your server every few seconds.
There's a better way. WebSockets give you a persistent, two-way connection between your Angular app and your server — meaning your dashboard updates the moment new data exists, with zero wasted requests.
In this article we'll build a complete real-time dashboard in Angular from scratch — WebSocket service, Signal-based components, auto-reconnection, and production-ready patterns.

How WebSockets Differ From Regular HTTP

Before writing any code, it's worth understanding what makes WebSockets special.

Regular HTTP:
Client → "Give me data" → Server
Client ← "Here's your data" ← Server
[Connection closes]

WebSocket:
Client ←→ Server [Connection stays open]
Server → "New data!" → Client (anytime)
Server → "More data!" → Client (anytime)
Client → "Send this" → Server (anytime

For a real-time dashboard showing live metrics, user activity, or financial data — the WebSocket model is a natural fit. The server pushes updates the moment they happen. No polling, no refresh button, no stale data.

Project Setup

For this article we'll build a dashboard that shows three live metrics: active users, requests per second, and server CPU usage.
Start with a fresh Angular 22 project:

ng new realtime-dashboard --standalone
cd realtime-dashboard
ng serve

RxJS ships with Angular so no extra dependencies are needed — webSocket from rxjs/webSocket handles everything.

Step 1 — Define Your Data Model

Start with a clear TypeScript interface for the data your server will push:

// core/models/dashboard.model.ts
export interface DashboardMetrics {
  activeUsers: number;
  requestsPerSecond: number;
  cpuUsage: number;
  timestamp: Date;
}

export interface MetricAlert {
  type: 'warning' | 'critical';
  metric: string;
  value: number;
  message: string;
}

export type DashboardEvent =
  | { kind: 'metrics'; payload: DashboardMetrics }
  | { kind: 'alert'; payload: MetricAlert };

Step 2 — The WebSocket Service

This is the heart of your real-time dashboard. One service manages the connection, handles errors, and exposes clean Observables for your components to consume:

// core/services/dashboard-socket.service.ts
import { Injectable, inject } from '@angular/core';
import { webSocket, WebSocketSubject } from 'rxjs/webSocket';
import {
  Observable,
  EMPTY,
  timer,
  Subject,
  switchMap,
  catchError,
  filter,
  map,
  shareReplay,
  takeUntil
} from 'rxjs';
import { DashboardEvent, DashboardMetrics, MetricAlert } from '../models/dashboard.model';
import { environment } from '../../environments/environment';

@Injectable({ providedIn: 'root' })
export class DashboardSocketService {
  private socket$!: WebSocketSubject<DashboardEvent>;
  private destroy$ = new Subject<void>();
  private connected = false;

  private readonly connection$: Observable<DashboardEvent> = timer(0, 3000).pipe(
    switchMap(() => {
      if (!this.connected) {
        this.socket$ = webSocket<DashboardEvent>(environment.wsUrl);
        this.connected = true;
      }
      return this.socket$.pipe(
        catchError(error => {
          console.error('[WebSocket] Connection error:', error);
          this.connected = false;
          return EMPTY;
        })
      );
    }),
    takeUntil(this.destroy$),
    shareReplay({ bufferSize: 1, refCount: true })
  );

  // Stream of live metrics
  metrics$: Observable<DashboardMetrics> = this.connection$.pipe(
    filter(event => event.kind === 'metrics'),
    map(event => (event as { kind: 'metrics'; payload: DashboardMetrics }).payload)
  );

  // Stream of alerts only
  alerts$: Observable<MetricAlert> = this.connection$.pipe(
    filter(event => event.kind === 'alert'),
    map(event => (event as { kind: 'alert'; payload: MetricAlert }).payload)
  );

  // Send a message to the server
  send(message: DashboardEvent): void {
    if (this.socket$ && this.connected) {
      this.socket$.next(message);
    }
  }

  disconnect(): void {
    this.destroy$.next();
    this.destroy$.complete();
    if (this.socket$) {
      this.socket$.complete();
    }
  }
}

A few things worth noting here:

shareReplay({ bufferSize: 1, refCount: true }) means multiple components can subscribe to the same connection without opening multiple WebSocket connections — crucial for a multi-widget dashboard.
Separating metrics$ and alerts$ into distinct streams lets each component subscribe only to what it needs.
The timer(0, 3000) pattern handles auto-reconnection: if the connection drops, it retries every 3 seconds.

Step 3 — A Metrics Store with Signals

Rather than subscribing directly in each component, create a Signal-based store that holds the latest dashboard state. This gives you one source of truth across your entire dashboard:

// core/stores/dashboard.store.ts
import { Injectable, signal, computed, inject, OnDestroy } from '@angular/core';
import { DashboardSocketService } from '../services/dashboard-socket.service';
import { DashboardMetrics, MetricAlert } from '../models/dashboard.model';

@Injectable({ providedIn: 'root' })
export class DashboardStore implements OnDestroy {
  private socketService = inject(DashboardSocketService);

  // Raw signals
  private _metrics = signal<DashboardMetrics | null>(null);
  private _alerts = signal<MetricAlert[]>([]);
  private _connected = signal(false);

  // Public readonly signals
  metrics = this._metrics.asReadonly();
  alerts = this._alerts.asReadonly();
  connected = this._connected.asReadonly();

  // Computed signals
  isCpuCritical = computed(() => (this._metrics()?.cpuUsage ?? 0) > 90);
  hasAlerts = computed(() => this._alerts().length > 0);
  latestAlert = computed(() => this._alerts()[0] ?? null);

  private metricsSub = this.socketService.metrics$.subscribe({
    next: data => {
      this._metrics.set(data);
      this._connected.set(true);
    },
    error: () => this._connected.set(false)
  });

  private alertsSub = this.socketService.alerts$.subscribe(alert => {
    this._alerts.update(current => [alert, ...current].slice(0, 10));
  });

  clearAlerts(): void {
    this._alerts.set([]);
  }

  ngOnDestroy(): void {
    this.metricsSub.unsubscribe();
    this.alertsSub.unsubscribe();
    this.socketService.disconnect();
  }
}

The store keeps the last 10 alerts with .slice(0, 10) — enough history to be useful without growing unbounded.

Step 4 — The Dashboard Component

With the store in place, the component becomes almost trivially simple:

// features/dashboard/dashboard.component.ts
import { Component, inject } from '@angular/core';
import { DashboardStore } from '../../core/stores/dashboard.store';
import { MetricCardComponent } from './components/metric-card.component';
import { AlertBannerComponent } from './components/alert-banner.component';
import { ConnectionStatusComponent } from './components/connection-status.component';

@Component({
  selector: 'app-dashboard',
  standalone: true,
  imports: [MetricCardComponent, AlertBannerComponent, ConnectionStatusComponent],
  template: `
    <app-connection-status [connected]="store.connected()" />

    @if (store.hasAlerts()) {
      <app-alert-banner [alert]="store.latestAlert()!" (dismiss)="store.clearAlerts()" />
    }

    @if (store.metrics(); as m) {
      <div class="metrics-grid">
        <app-metric-card
          label="Active users"
          [value]="m.activeUsers"
          unit="users"
          color="blue"
        />
        <app-metric-card
          label="Requests / sec"
          [value]="m.requestsPerSecond"
          unit="req/s"
          color="teal"
        />
        <app-metric-card
          label="CPU usage"
          [value]="m.cpuUsage"
          unit="%"
          [color]="store.isCpuCritical() ? 'red' : 'green'"
        />
      </div>
    } @else {
      <div class="loading">Connecting to server...</div>
    }
  `
})
export class DashboardComponent {
  store = inject(DashboardStore);
}

The component has zero business logic. It just reads from the store and renders. That's exactly how it should be.

Step 5 — The Metric Card Component

A clean, reusable card that displays a single metric:

// features/dashboard/components/metric-card.component.ts
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-metric-card',
  standalone: true,
  template: `
    <div class="card" [class]="'card--' + color()">
      <span class="card__label">{{ label() }}</span>
      <span class="card__value">{{ value() | number:'1.0-0' }}</span>
      <span class="card__unit">{{ unit() }}</span>
    </div>
  `,
  styles: [`
    .card {
      padding: 24px;
      border-radius: 12px;
      background: var(--card-bg, #f8f9fa);
      border-left: 4px solid var(--card-accent, #6c757d);
      transition: border-color 0.3s ease;
    }
    .card__label { font-size: 12px; color: #6c757d; text-transform: uppercase; }
    .card__value { font-size: 36px; font-weight: 500; display: block; }
    .card__unit  { font-size: 14px; color: #6c757d; }
    .card--blue  { --card-accent: #0d6efd; }
    .card--teal  { --card-accent: #0dcaf0; }
    .card--green { --card-accent: #198754; }
    .card--red   { --card-accent: #dc3545; }
  `]
})
export class MetricCardComponent {
  label = input.required<string>();
  value = input.required<number>();
  unit  = input.required<string>();
  color = input<string>('blue');
}

Step 6 — Handling the Connection Status

A small but important UX detail — always show the user whether they're connected:

// features/dashboard/components/connection-status.component.ts
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-connection-status',
  standalone: true,
  template: `
    <div class="status" [class.status--connected]="connected()">
      <span class="dot"></span>
      {{ connected() ? 'Live' : 'Reconnecting...' }}
    </div>
  `,
  styles: [`
    .status { display: flex; align-items: center; gap: 6px; font-size: 13px; color: #dc3545; }
    .status--connected { color: #198754; }
    .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
    .status--connected .dot { animation: pulse 2s infinite; }
    @keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.4} }
  `]
})
export class ConnectionStatusComponent {
  connected = input.required<boolean>();
}

Simulating a WebSocket Server for Testing

Don't have a real WebSocket server? Here's a minimal Node.js mock to test against:

// server/mock-ws.js
const { WebSocketServer } = require('ws');
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', ws => {
  console.log('Client connected');

  const interval = setInterval(() => {
    const metrics = {
      kind: 'metrics',
      payload: {
        activeUsers: Math.floor(Math.random() * 1000) + 500,
        requestsPerSecond: Math.floor(Math.random() * 200) + 50,
        cpuUsage: Math.floor(Math.random() * 100),
        timestamp: new Date()
      }
    };
    ws.send(JSON.stringify(metrics));

    // Occasionally send an alert
    if (Math.random() < 0.1) {
      ws.send(JSON.stringify({
        kind: 'alert',
        payload: {
          type: 'warning',
          metric: 'cpuUsage',
          value: 95,
          message: 'CPU usage is critically high'
        }
      }));
    }
  }, 1000);

  ws.on('close', () => clearInterval(interval));
});

console.log('Mock WebSocket server running on ws://localhost:8080');

Run it with node server/mock-ws.js and point your environment.wsUrl to ws://localhost:8080

Production Checklist

Before shipping your real-time dashboard to production:
Authentication

Pass your auth token as a query parameter or handle it on the first message:

webSocket(`wss://api.example.com/dashboard?token=${authToken}`)

Exponential backoff

Replace the fixed 3-second retry with exponential backoff to avoid hammering your server during outages:

import { retryWhen, delay, scan } from 'rxjs';

retryWhen(errors => errors.pipe(
  scan((retryCount, _) => retryCount + 1, 0),
  delay(attempt => Math.min(1000 * 2 ** attempt, 30000))
))

Environment configuration

// environments/environment.prod.ts
export const environment = {
  production: true,
  wsUrl: 'wss://api.yourapp.com/dashboard'
};

For a dashboard where you only receive data (no need to send back), Server-Sent Events (SSE) is actually a simpler alternative worth considering. But if you need bidirectional communication — sending filters, subscribing to specific metrics, sending user actions — WebSockets are the right choice

Building a real-time Angular dashboard with WebSockets is more accessible than it looks. The combination of RxJS's webSocket(), Angular Signals for state, and a clean store architecture gives you a production-ready foundation that scales as your dashboard grows.
The key patterns to take away:

  • One WebSocketSubject shared across all components via shareReplay

  • A Signal-based store as the single source of truth

  • Auto-reconnection built into the service layer

  • Components that are pure — just reading from the store and rendering

Your users deserve data that's always fresh. Give them that — and retire the refresh button for good.

Have you built real-time features in Angular? What approach did you use — WebSockets, SSE, or polling? Share your experience below!