TinyBase logoTinyBase

onMessage

The onMessage method is called when a message is handled by the server.

onMessage(
  fromClientId: string,
  toClientId: string,
  remainder: string,
): void
TypeDescription
fromClientIdstring

The Id of the client that send the message.

toClientIdstring

The Id of the client to receive the message (or empty for a broadcast).

remainderstring

The remainder of the body of the message.

returnsvoid

This has no return value.

This is useful if you want to debug the synchronization process, though be aware that this method is called very frequently. It is called with the Id of the client the message came from, the the Id of the client the message is to be forwarded to, and the remainder of the message itself.

Since this method is called often, it should be performant. The path Id is not passed as an argument, since it has a small cost to provide by default. You can use the getPathId method yourself if that information is needed.

Example

This example logs every message routed by the Durable Object between clients.

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

export class MyDurableObject extends WsServerDurableObject {
  onMessage(fromClientId, toClientId, remainder) {
    console.info(
      `Message from '${fromClientId}' to '${toClientId}': ${remainder}`,
    );
  }
}

Since

v5.4.0