TinyBase logoTinyBase

getClientIds

The getClientIds method method returns the active clients that the WsServer is handling for a given path.

getClientIds(pathId: string): Ids
TypeDescription
pathIdstring

The path for which to return the list of active clients.

returnsIds

An array of the clients connected to the given path.

Example

This example creates a WsServer, sets some clients up to connect to it, and then gets the number of clients on the given paths. (The client Ids themselves are unique, based on the sec-websocket-key header.)

import {WebSocket, WebSocketServer} from 'ws';
import {createMergeableStore} from 'tinybase';
import {createWsServer} from 'tinybase/synchronizers/synchronizer-ws-server';
import {createWsSynchronizer} from 'tinybase/synchronizers/synchronizer-ws-client';

const server = createWsServer(new WebSocketServer({port: 8047}));

const synchronizer1 = await createWsSynchronizer(
  createMergeableStore(),
  new WebSocket('ws://localhost:8047/roomA'),
);
const synchronizer2 = await createWsSynchronizer(
  createMergeableStore(),
  new WebSocket('ws://localhost:8047/roomA'),
);
const synchronizer3 = await createWsSynchronizer(
  createMergeableStore(),
  new WebSocket('ws://localhost:8047/roomB'),
);

console.log(server.getClientIds('roomA').length);
// -> 2
console.log(server.getClientIds('roomB').length);
// -> 1

synchronizer3.destroy();
// ...
console.log(server.getClientIds('roomB').length);
// -> 0

synchronizer1.destroy();
synchronizer2.destroy();
server.destroy();

Since

v5.0.0