Guide

Custom Events

Send custom data to your WebSocket client with C# actions

C# Methods

You can send custom events to your WebSocket client utilizing these C# methods in Streamer.bot:

CPH.WebsocketBroadcastString(string data)
CPH.WebsocketBroadcastJson(string data)

Handling Custom Events

Custom events are sent with event source General and event type of Custom.

You can easily subscribe to your custom events using the .on() method:

example.ts
// Subscribe to all custom events from the connected Streamer.bot instance
client.on('General.Custom', (data) => {
  console.log('Custom Event:', data);
});

Example

We're going to utilize custom events to drive a media overlay from Streamer.bot.

Here we are using an event key within our JSON to allow us to easily filter our related events on the client side.

We are also going to define an action key in our JSON payload to request a specific action from the client.

example.csharp
string json = "{'event': 'mediaPlayer', 'action': 'pause'}".Replace("'", "\"");
CPH.WebsocketBroadcastJson(json);

On the client side, we will handle these events with the following event handler:

example.ts
client.on('General.Custom', (payload) => {
  if (payload.data.event === 'mediaPlayer' && payload.data.action === 'pause') {
    // handle media player pause events in here :)
  }
});