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

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
U
Unit 42
博客园 - 叶小钗
博客园 - 聂微东
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
D
DataBreaches.Net
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
美团技术团队
The Cloudflare Blog
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
S
Schneier on Security
C
Check Point Blog
Project Zero
Project Zero
The Hacker News
The Hacker News
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
SecWiki News
SecWiki News
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
H
Help Net Security
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
T
Troy Hunt's Blog
H
Hacker News: Front Page
Vercel News
Vercel News

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 Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Building Real-Time Dashboards in Angular with WebSockets — A Complete Guide
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!