All posts
AWS Architecture

Next.js WebSockets on Vercel: upgradeWebSocket, Redis Fan-Out, and Surviving Deploys

By

November 2, 2026

5 min read

Next.js has no WebSocket API, so Vercel added upgradeWebSocket() to @vercel/functions 3.10.0. Getting an echo server running takes minutes; a chat or presence feature that works across instances and deploys needs four more pieces. A build order for a production-ready Next.js realtime feature on Vercel.

WebSockets
Next.js
Vercel
Realtime

In a Next.js route handler, a GET that returns upgradeWebSocket((ws) => { ... }) from @vercel/functions (version 3.10.0 or later) accepts a WebSocket connection, and ws.on('message') and ws.send() work as you'd expect. That is the echo server. The rest of this post is what a real feature needs on top.

Client

WebSocket with reconnect and backoff
Last-seen message ID
Resubscribe on open

Edge

Vercel Firewall rules on the upgrade path
Rate limit on upgrades

Rules and limits apply to the upgrade request, not to messages.

Route handler

upgradeWebSocket()
Auth check on upgrade
Idempotent message handlers

Pinned to one instance until the connection closes at max duration.

Shared state

Redis pub/sub or Streams for fan-out
Presence in sorted sets

The only way a message reaches sockets on other instances.

Durable store

Postgres or Redis Streams for history
Sequence numbers for catch-up

A Next.js realtime feature on Vercel

Build order

  1. Authenticate on upgrade: read the session from the request before accepting, and close with an error code the client understands.

  2. Store first, then broadcast: write each message to the durable store with a sequence number, then publish it to Redis so every instance relays it to its own sockets.

  3. Reconnect on the client with exponential backoff capped at about 30 seconds, plus random jitter, then send the last sequence number seen and replay what was missed.

  4. Set maxDuration deliberately. Longer means fewer reconnects; shorter means old deployments drain faster. Either way the client must expect the close.

  5. Version your messages, because for up to the max duration two deployments are talking to clients at once.

  6. Load-test a reconnect storm: close every connection at once and watch the upgrade rate limit, Redis and your database.

Building this into a Next.js app?

We'll review your design before you write the reconnect logic, which is where most of the time goes. Free, 30 minutes.

hoolco Monthly Review

Once a month: what changed in tech that month and what it means for you, from cloud and AI to security and the tools you build with. Every item links to its source. No sales pitch, unsubscribe in one click.

As of November 2, 2026. Provided as is, for general information, and accurate to the best of our knowledge on the date shown. Not legal, compliance, tax, financial or other professional advice. Prices, limits and vendor services change; check current vendor documentation before relying on anything here. Terms of use.