RTCForge API Reference v1.1.1
    Preparing search index...

    Class HashRing

    Consistent-hashing ring for distributing keys across a set of weighted nodes.

    HashRing uses rendezvous (highest-random-weight) hashing: for a given key it computes a score for every node and returns the highest-scoring one. This yields a stable mapping in which adding or removing a node only remaps the keys that node is responsible for, leaving the rest untouched — ideal for sharding rooms, sessions, or state across a cluster.

    Weights bias the scoring so heavier nodes receive proportionally more keys. The hash is a deterministic 53-bit function of the key and node id, so the same inputs always map the same way regardless of insertion order.

    const ring = new HashRing(['sfu-1', 'sfu-2', { id: 'sfu-3', weight: 2 }])
    const owner = ring.get('room:abc') // node responsible for this room
    const replicas = ring.getN('room:abc', 2) // primary + one backup
    ring.remove('sfu-2') // only sfu-2's keys move
    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    Methods

    • Adds a node to the ring, or updates the weight of an existing node with the same id.

      Parameters

      • node: string | RingNode

        A node id string (weight 1) or a RingNode with an explicit weight.

      Returns void

      Error if node is a RingNode with a weight less than or equal to 0.

    • Returns the single node responsible for key.

      Parameters

      • key: string

        The key to route (e.g. a room or session identifier).

      Returns string | undefined

      The id of the highest-scoring node, or undefined if the ring is empty.

    • Returns the top n nodes responsible for key, in descending score order.

      Parameters

      • key: string

        The key to route.

      • n: number

        The maximum number of nodes to return.

      Returns string[]

      Up to n node ids ordered from most to least preferred; empty if n is not positive or the ring is empty. Useful for selecting a primary plus replicas.

    • Checks whether a node with the given id is on the ring.

      Parameters

      • id: string

        The node id to test.

      Returns boolean

      true if the node is present.

    • Returns the ids of all nodes currently on the ring.

      Returns string[]

      A new array of node ids, in insertion order.

    • Removes a node from the ring.

      Parameters

      • id: string

        The id of the node to remove.

      Returns boolean

      true if a node was removed, false if no node with that id existed.