# Building a WebSocket server: what production needs, and when to use a hosted service

> A WebSocket server fits in twenty lines; running one for real users means TLS, open-file limits, heartbeats, authorization, fan-out across servers and safe deploys. The checklist, and when a hosted service fits.

Source: https://hibira.io/topics/build-websocket-server
Updated: 2026-11-18

A WebSocket server that echoes messages fits in twenty lines. A WebSocket server that thousands of users rely on is mostly everything around those lines: TLS, open-file limits, heartbeats, authorization, fan-out between servers and deploys that do not drop everyone at once. This page walks through that list, so you can decide what to build yourself and what to hand to a hosted service.

## The twenty-line version

With Node.js and the [ws library](https://github.com/websockets/ws):

```js
import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', (socket) => {
  socket.on('message', (data) => {
    // Send what one client wrote to every connected client.
    for (const client of wss.clients) {
      if (client.readyState === 1) client.send(data.toString());
    }
  });
});
```

This works on your laptop. It has no rooms, no idea who the user is, and every message goes to everyone on this one process. The protocol itself is [RFC 6455](https://www.rfc-editor.org/rfc/rfc6455); the hard part is not the handshake but running it for real users.

## What production adds

| Need | Why it matters | What you set up |
| --- | --- | --- |
| TLS and a proxy | Browsers on HTTPS pages only open `wss://` connections | A certificate, and a proxy that passes the `Upgrade` and `Connection` headers |
| Open-file limits | Every connection holds a file descriptor; many systems default to 1,024 | `ulimit -n`, the process manager's limit and the proxy's worker limits |
| Heartbeats | Mobile networks and load balancers drop idle connections without telling either side | Ping/pong on a timer, and client code that reconnects with backoff |
| Channels and authorization | Users must only receive their own orders, chats and rooms | Channel names, a signed subscribe step checked against your app's sessions |
| Fan-out across servers | One process has a ceiling; a second server does not see the first one's clients | Redis or NATS pub/sub between servers, and a load balancer in front |
| Presence | "Who is online" must survive reconnects and servers restarting | Member lists with expiry, shared between servers |
| Deploys | Restarting the process disconnects every user at once | Graceful drain, and clients that reconnect at random intervals |
| Monitoring | Problems show up as silent disconnects, not errors | Connection and message counts, and alerts on drops |

```text
flow: One process is simple; a second server needs pub/sub between them
== One server
Browser --wss://--> Proxy|TLS, Upgrade --ws://--> WebSocket process|channels, auth
== Several servers
Browser --wss://--> Load balancer --> WebSocket server A --Redis pub/sub--> WebSocket server B
```

## The parts people underestimate

**Reconnects.** Every client will reconnect many times a day: phones change networks, laptops sleep, servers restart. After a reconnect the client must subscribe again and fetch what it missed from your API. If every client reconnects the instant a server restarts, the new process is hit by all of them at once, so add a random delay.

**Slow clients.** A client on a weak connection reads slowly while the server keeps writing. Without a limit on each connection's send buffer, one slow phone can grow the process's memory until it fails.

**Authorization at subscribe time.** The WebSocket connection usually starts from a page that already has a session, but the server still has to check, for each private channel, that this user may listen. Pusher-protocol servers do this with a signature from your own backend, so the WebSocket server never needs your database.

## Build it, or use a hosted service

Building it yourself fits when:

- realtime is your product, and you need a custom protocol or binary frames;
- you already run Redis, a load balancer and on-call for other services;
- your traffic is steady and predictable.

A hosted Pusher-compatible service fits when:

- realtime is one feature among many (notifications, chat, live status, dashboards);
- you want libraries for the browser, iOS, Android and Flutter that already handle reconnects;
- nobody on the team wants to be paged for WebSocket servers.

With a hosted service, the server code above becomes one HTTP call from your backend and a subscription in the client:

```js
// Client: pusher-js
const pusher = new Pusher('YOUR_APP_KEY', { cluster: 'hkg', wsHost: 'ws.hibira.io', forceTLS: true });
pusher.subscribe('private-orders-42').bind('order.shipped', (e) => showStatus(e));
```

The protocol is described on the [Pusher protocol page](https://hibira.io/pusher-protocol), and [Socket.IO vs Pusher](https://hibira.io/topics/socket-io-vs-pusher) compares running Socket.IO yourself with a hosted service in more detail. Library versions were checked on 2026-11-18.

## Questions

### How many connections can one WebSocket server hold?

It depends on memory and on what each connection does, not on the protocol. Idle connections are cheap; the usual first limit is the open-file setting, which is often 1,024 until you raise it. Measure with your own messages before you plan capacity.

### Do I need Redis for WebSockets?

Not with one server. As soon as you run two, a message published on server A must reach clients connected to server B, and that needs pub/sub between them. Redis is the common choice; NATS and Postgres `LISTEN/NOTIFY` also work at smaller scale.

### Can I start with my own server and move to a hosted service later?

Yes, if your own server speaks the Pusher protocol (Laravel Reverb, Soketi and others do). Then moving is a change of host, key and secret, with the same client and server libraries. See [how to migrate](https://hibira.io/migrate).

### Is a WebSocket server the same as Socket.IO?

No. Socket.IO is a library with its own protocol on top of WebSocket, so a plain WebSocket client cannot talk to a Socket.IO server. Pusher-protocol servers also add their own message format, which their client libraries handle for you.
