Watch / Unwatch channels

You can let users watch a given channel and its messages, without the need to join the channel as members. Similarly, you can stop watching the channel without the need to leave the channel.

Watch a channel

Connect() lets users watch a given channel and its messages. After connecting, the OnMessageReceived event is triggered when a message is received. You must handle the event to receive the message.

Method signature

channel.Connect()

Event signature

public event Action<Message> OnMessageReceived;

Event handler signature

void EventHandler(Message message)
ParameterTypeRequiredDefaultDescription
messageMessageYesn/aThe received message.

Output

This method doesn't return any data. To receive a message, you must handle the OnMessageReceived event.

Basic usage

Start receiving messages on the support channel.

if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};

channel.OnMessageReceived += OnMessageReceivedHandler; // or use lambda

void OnMessageReceivedHandler(Message message)
{
Console.WriteLine($"Message received: {message.MessageText}");
}

channel.Connect();

Unwatch a channel

Disconnect() lets users unwatch a given channel and its messages.

Method signature

channel.Disconnect()

Output

This method doesn't return any data.

Basic usage

Stop receiving messages from a channel after receiving the first message.

void OnMessageReceivedHandler(Message message)
{
Console.WriteLine($"Message received: {message.MessageText}");
channel.Disconnect();
Console.WriteLine("Disconnected from the channel.");
}
Last updated on