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 a list with a doorbell: add or remove an item and interested code receives an event.

When it fits

When should I use it?

Use it for simple application-managed collections with added/removed events. Use RoomRegistry for bounded socket membership or connectedClients for page projections.

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
import { SocketRegistry } from 'redweb'

const players = new SocketRegistry()
players.on('added', player => console.log('Joined:', player.id))
players.add({ id: 'alice' })
players.remove('alice')
  1. 1

    add stores an item and emits added; duplicate items are not rejected.

  2. 2

    remove matches an object by identity or an identifier by the chosen property, then emits removed.

  3. 3

    all returns a shallow array copy. The application owns limits and cleanup.

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`.