Skip to content

BroadcastOthers Decorator

The broadcastOthers decorator enables you to broadcast a message to every peer on your namespace (excluding the peer).

The decorator accepts a string event - This is the event that will be sent back to the client along with the data.

Usage

typescript
@Namespace('/foo') // register the namespace
@Controller('/api/playground')
export default class DummyController {
  @BroadcastOthers('test')
  public async onMessage(incomingMessage: unknown, peer: { id: string; ip: string; }): Promise<Record<string, string>> {
    // { foo: 'bar' } will be emitted to every peer on the namespace
    // excluding the sender
    return { foo: 'bar' }
  }
}

On your client side, messages will be received with the following pattern:

typescript
{ 
  event: 'test', // the event you specified when using the decorator
  data: { foo: 'bar' } // the data returning from your function
};

See Also