TinyBase logoTinyBase β

getClientAccess

The getClientAccess method is used to get the ClientAccess object that the authorize method returned for a client when it joined the path.

getClientAccess(clientId: string): ClientAccess | undefined
TypeDescription
clientIdstring

The Id of the client.

returnsClientAccess | undefined

The ClientAccess object for the client, or undefined if there is no such client, or if it is being closed and so no longer has any access.

This is useful for when you want to know which of the Durable Object's clients belong to a particular user - to close them, for example. Like the access itself, it survives the Durable Object hibernating.

If clients are not authorized, this is an empty ClientAccess object.

Example

This example adds a method to a Durable Object that a Worker can call to see which users are connected.

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

export class MyDurableObject extends WsServerDurableObject {
  authorize(pathId, request) {
    return {
      context: {user: new URL(request.url).searchParams.get('user')},
    };
  }

  getUsers() {
    return this.getClientIds().map(
      (clientId) => this.getClientAccess(clientId)?.context.user,
    );
  }
}

Since

v10.2.0