TinyBase logoTinyBase

onClientId

The onClientId method is called when a client connects to, or disconnects from, the server.

onClientId(
  pathId: string,
  clientId: string,
  addedOrRemoved: IdAddedOrRemoved,
): void
TypeDescription
pathIdstring

The Id of the path being served by the Durable Object.

clientIdstring

The Id of the client joining or leaving.

addedOrRemovedIdAddedOrRemoved

Whether the client is joining or leaving.

returnsvoid

This has no return value.

This method is called with the path Id, the client Id, and an IdAddedOrRemoved flag indicating whether it this is being triggered by the client joining (1) or the client leaving (-1).

Note that if you call the getClientIds method from within this method as a client is getting removed, it will still be returned in the list of client Ids.

Example

This example logs every client that joins (the client Id is 'added') or leaves (the client Id is 'removed') on the path being served by the Durable Object.

import {WsServerDurableObject} from 'tinybase/synchronizers/synchronizer-ws-server-durable-object';

export class MyDurableObject extends WsServerDurableObject {
  onClientId(pathId, clientId, addedOrRemoved) {
    console.info(
      (addedOrRemoved ? 'Added' : 'Removed') +
        ` client ${clientId} on path ${pathId}`,
    );
  }
}

Since

v5.4.0