Skip to main content

Node.js WebSocket Real-Time Server Builder

Build a scalable WebSocket server with rooms, authentication, presence tracking, and horizontal scaling across multiple nodes.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt builds a scalable WebSocket server for [use_case] using [ws_library] with Node.js and TypeScript. It covers server setup integrated with [http_framework], connection authentication via [auth_method], room and channel management, a presence system with heartbeat, typed messages ([message_types]) as discriminated unions, horizontal scaling across [instance_count] nodes via [scaling_adapter], reconnection with message buffer and replay, binary support for [binary_use_case], per-connection rate limiting, monitoring, and a client-side TypeScript SDK with auto-reconnect.

The structure works because the gap between a WebSocket demo and a production system is exactly the hard parts this prompt names: scaling across nodes and reconnection-with-replay. A single-node socket server is trivial; broadcasting correctly across [instance_count] instances requires [scaling_adapter], and surviving a deploy requires buffering messages and replaying them on reconnect. By forcing those into the spec, the output addresses real failure modes instead of a happy-path demo.

When to use it

  • When building real-time features like collaborative editing, chat, or live dashboards
  • When connections must be authenticated via [auth_method] before joining rooms
  • When you need presence (who is online) per room with heartbeat detection
  • When scaling beyond one node and broadcasts must reach all [instance_count] instances
  • When clients must survive a deploy or network blip without losing messages
  • When you want a typed client SDK so frontend events match the server contract
  • When a single misbehaving connection could flood a room and you need per-connection rate limiting

Example output

You get a TypeScript WebSocket server: setup wired to [http_framework], auth on the handshake validating [auth_method], room join/leave/broadcast logic, the presence and heartbeat system tracking online users per room, the [message_types] discriminated unions, the [scaling_adapter] configuration for multi-node broadcast across [instance_count] nodes, reconnection-with-replay handling, binary support for [binary_use_case], per-connection rate limiting, and a typed client SDK with auto-reconnect. It includes connection metrics — active connections, messages per second, errors, memory usage — rather than only the bare socket plumbing, so the result is closer to a deployable real-time backend for [use_case] than a single-file demo.

Pro tips

  • Use the [scaling_adapter] (Redis adapter for Socket.IO) as soon as [instance_count] exceeds one; without it, a broadcast only reaches clients on the same node
  • Validate [auth_method] on the handshake, not after connect, so unauthorized sockets never join a room
  • Test reconnection-with-replay against a real deploy; this path is what separates a demo from something that survives shipping
  • Keep [message_types] as discriminated unions so the client SDK and server agree on every event shape at compile time
  • Only enable binary support if [binary_use_case] truly needs it; for text-only payloads it adds complexity for no gain
  • Set the per-connection rate limit deliberately so a single misbehaving client cannot flood a room

Frequently Asked Questions

Why do I need a Redis adapter for WebSockets?
When you run more than one node (set via `[instance_count]`), a broadcast on one node does not reach clients connected to another. The `[scaling_adapter]`, such as the Socket.IO Redis adapter, relays messages between nodes so broadcasts reach every connected client.
How are clients authenticated on connection?
Authentication happens on the handshake using whatever you set in `[auth_method]`, for example a JWT in the handshake query. Validating before the connection is established means unauthorized sockets never get to join rooms or receive messages.
Does it handle clients reconnecting after a deploy?
Yes. It includes reconnection handling with a message buffer and replay, so messages sent while a client was briefly disconnected are delivered on reconnect. Test this path carefully, as it is what makes the server survive deploys gracefully.
Can I use a WebSocket library other than the default?
Yes. The `[ws_library]` variable lets you choose Socket.IO, ws, or another library. Note that the `[scaling_adapter]` and SDK details depend on the library you pick, so the scaling approach changes accordingly.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Node.js & TypeScript Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support