WebSocket · In depth

SocketRegistry

Small EventEmitter-backed list for socket-scoped entities (players, rooms, etc.). Emits `added` and `removed` events.

Explain it like I’m five

The simple mental model

SocketRegistry is the route’s attendance sheet. It knows which sockets are active and provides a controlled way to visit or disconnect them.

When it fits

When should I use it?

Use it when route logic needs bounded connection tracking, fan-out, observability, or coordinated draining.

A practical API pattern

Follow the example

This pattern explains the named API. Application classes, credentials, and assets may need to be supplied; use a complete recipe for a runnable starting point.

Read this article as Markdown

JavaScript
const { SocketRegistry } = require('redweb')

class PlayerRegistry extends SocketRegistry {
  addPlayer(player) {
    this.add(player)
    this.emit('playerJoined', player)
  }
}
  1. 1

    Accepted connections are registered once and removed during close cleanup.

  2. 2

    Iteration works over the route-owned set rather than an application-global list.

  3. 3

    Shutdown can stop admissions, notify clients, and close the remaining registry deterministically.

Surface area

Methods and members

add(item)

Stores the item and emits an `added` event.

remove(itemOrId, by = "id")

Remove by object reference or by matching a property (defaults to "id"); returns true when removal occurred and emits `removed`.

all()

Returns a shallow copy of all stored items.

count()

Convenience getter for `all().length`.