CLAUDE LABJP
FORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as OctoberFORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as October
Articles/API & SDK
API & SDK/2026-04-26Advanced

Building a Scalable Real-Time AI Chat Server with Claude API × WebSocket × Redis Pub/Sub — Node.js Production Architecture, Multi-User Management, and Cost Control

Running a real-time AI chat server on Claude API, WebSocket, and Redis Pub/Sub in production. SSE trade-offs, multi-instance routing, and how stop latency quietly corrupts per-user budgets — with the instrumentation code to measure it.

websocketredis2nodejs2streaming21realtime3production111api38

Premium Article

When users press send in a chat UI and watch an AI response stream in word by word, that experience is often implemented with SSE (Server-Sent Events). But the moment requirements expand to "let users interrupt mid-generation," "manage multiple turns without reconnecting," or "show bidirectional typing indicators," SSE hits a hard wall.

SSE is an HTTP response extension — one-way, server to client only. Any client-to-server signal still needs a separate REST API call. Session state has to live somewhere. The moment you scale horizontally across multiple instances, routing messages to the right server becomes non-trivial.

I ran into exactly this when building the backend for a mobile app. The single-instance prototype worked fine, but the first time I tried to add "stop generation" functionality, I realized the architecture needed rethinking from the ground up.

WebSocket was designed for bidirectional communication. One persistent connection handles sending and receiving, which makes conversation state management much simpler. Add Redis Pub/Sub and you can route messages correctly regardless of how many server instances you're running.

This guide builds a production-quality real-time AI chat server using Claude API streaming + WebSocket + Redis Pub/Sub. Every section includes working code you can run.

SSE vs WebSocket: The Decision Framework

Getting this choice wrong early means a significant refactor later. Here's how to think through it.

SSE is sufficient when:

  • Users send a message and wait passively for a complete response
  • You need server-to-client push only (news feeds, progress indicators, notifications)
  • Your backend runs on Cloudflare Workers, Lambda, or another environment where WebSocket is awkward to manage
  • HTTP/2 is available and connection limits aren't a concern

WebSocket becomes necessary when:

  • Users need to interrupt AI generation mid-stream (the "stop" button that actually works)
  • You want to send "user is typing" signals from the client to influence AI behavior
  • You need to manage multi-turn conversations over a single connection to reduce reconnect overhead
  • Multiple users share a session for collaboration features
  • You're building for mobile, where connection lifecycle management benefits from explicit control

This guide targets the second set of use cases. If your requirements are firmly in the first group, SSE is simpler — don't add complexity you don't need.

System Architecture Overview

Here's the full picture of what we're building:

[Mobile/Web Client]
       |  WebSocket (wss://)
       |
[Node.js WebSocket Server (horizontally scaled)]
       |  Claude API Streaming (HTTPS/SSE)
       |  Redis Pub/Sub (cross-instance message routing)
       |  Redis String/Hash (per-user token budget + history TTL)
       |
[Redis Cluster] --- [Anthropic Claude API]

Each component has a single responsibility:

  • Node.js WebSocket Server: Accepts connections, validates JWT tokens, forwards user messages to Claude API as streaming requests, and pipes response deltas back to the client in real time
  • Redis Pub/Sub: When running multiple server instances, acts as a message bus so any instance can deliver to any user regardless of which instance they're connected to
  • Redis Key/Value: Stores per-user monthly token usage counters with TTL, and optionally short-term conversation history for reconnect resumption

Install the required packages:

npm install ws @anthropic-ai/sdk ioredis jsonwebtoken dotenv
npm install -D typescript @types/ws @types/node tsx

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Instrumentation for measuring post-stop billing, and what 100ms vs 500ms vs 2,000ms of stop latency costs across 3,000 interruptions a month
Why a stop signal sent only over Redis Pub/Sub disappears during instance restarts, and the local-first fix
Complete implementation: WebSocket server, Claude API stream bridging, Redis routing, and JWT budget control (Node.js / TypeScript)
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API & SDK2026-03-31
Claude API Streaming × Real-Time Chat UI: Production Implementation Guide
A practical guide to running Claude API streaming with Server-Sent Events in Next.js App Router at production grade, with measured latency, recovery patterns, and Cloudflare Workers edge-relay details from real indie operation
API & SDK2026-06-27
When Claude API Streaming Stops Without an Error: Detecting Silent Stalls and Resuming Mid-Stream
How to catch the 'silent stall' where Claude API streaming stops with no exception at all, using a content-level watchdog that times the gap between tokens, plus a resume path that carries received text forward as an assistant prefill, and a four-layer timeout budget for long-running automation.
API & SDK2026-07-03
How Many Concurrent Claude API Requests Can You Actually Hold? Sizing Production Infrastructure with Little's Law and Measured Memory
Concurrency, queue depth, and memory are numbers you can derive, not guess. A working method for sizing Claude API production deployments with Little's Law, a memory probe, and a 30-minute load check — learned the hard way from an OOM crash.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →