onClientId
The onClientId method is called when a client connects to, or disconnects from, the server.
onClientId(
pathId: string,
clientId: string,
addedOrRemoved: IdAddedOrRemoved,
): void| Type | Description | |
|---|---|---|
pathId | string | The |
clientId | string | The |
addedOrRemoved | IdAddedOrRemoved | Whether the client is joining or leaving. |
| returns | void | 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 == 1 ? 'Added' : 'Removed') +
` client ${clientId} on path ${pathId}`,
);
}
}
Since
v5.4.0