SocketRegistry

Documentation for Redweb 0.13.0. Install that exact version when following these examples.

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

Explain it like I’m five

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

When should I use it?

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

Follow the example

This API pattern illustrates the named surface; it may require application-owned classes, credentials, or assets. Start from a complete recipe for a runnable application.

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

class PlayerRegistry extends SocketRegistry {
  addPlayer(player) {
    this.add(player)
    this.emit('playerJoined', player)
  }
}
  1. Accepted connections are registered once and removed during close cleanup.
  2. Iteration works over the route-owned set rather than an application-global list.
  3. Shutdown can stop admissions, notify clients, and close the remaining registry deterministically.

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.

What should I watch for?

Do not retain sockets in parallel collections without cleanup. Prefer rooms or socket context for indexes with clear ownership.