Stream channels

Updated

Stream messages to and from a room.

Stream channels are based on the room model. In Signaling, communication through stream channels requires use of specific APIs. This page shows you how to join, leave, and send messages in stream channels.

Understand the tech

To use stream channels, you first create a stream channel object instance. The channel instance gives you access to all stream channel management methods. Use the stream channel instance to:

  • Join and leave a channel
  • Join and leave topics
  • Subscribe to and unsubscribe from topics
  • Send messages
  • Destroy the channel instance

After joining a stream channel, you listen to event notifications in the channel. To send and receive messages in the channel, you use topics. Signaling allows thousands of stream channels to exist simultaneously in your app. However, due to client-side performance and bandwidth limitations, a single client may only join a limited number of channels concurrently. For details, see API usage restrictions.

Prerequisites

Ensure that you have:

  • Integrated the Signaling SDK in your project, and implemented the framework functionality from the SDK quickstart page.

  • Activated the stream channel capability.

    Important

    Stream channel activation requirements

    Please note that the activation of Stream Channel functionality in Signaling directly depends on the activation of the 128-host feature in Real-Time Communication (RTC). To ensure proper activation:

    • Submit a formal request to support@agora.io.
    • Specifically request activation of the 128-host feature.
    • Wait for confirmation before implementing Stream Channel features.

    For further assistance or questions regarding this requirement, please contact Agora Support.

Implement communication in a stream channel

This section shows you how to use the Signaling SDK to implement stream channel communication in your app.

Create a stream channel

To use stream channel functionality, call CreateStreamChannel to create a IStreamChannel object instance.

IStreamChannel streamChannel = rtmClient.CreateStreamChannel("chat_room");

This method creates only one IStreamChannel instance at a time. If you need to create multiple instances, call the method multiple times.

// Create the first instance
IStreamChannel streamChannel1 = rtmClient.CreateStreamChannel("chat_room1");
// Create the second instance
IStreamChannel streamChannel2 = rtmClient.CreateStreamChannel("chat_room2");

Signaling enables you to create unlimited stream channel instances in a single app. However, best practice is to create channels based on your actual requirements to maintain optimal client-side performance. For instance, if you hold multiple stream channel instances, destroy the ones that are no longer in use to prevent resource blocking, and recreate them when they are needed again.

Join a stream channel

Call the JoinAsync method on the IStreamChannel instance with appropriate options as follows:

var options = new JoinChannelOptions();
options.token = "your_token";
var (status,response) = await streamChannel.JoinAsync(options);
if (status.Error)
{
    Debug.Log(string.Format("{0} is failed, ErrorCode: {1}, due to: {2}", status.Operation, status.ErrorCode, status.Reason));
}
else
{
    Debug.Log(string.Format("User:{0} Join stream channel success! at Channel:{1}", response.UserId, response.ChannelName));
}

When joining a channel, set the token parameter in JoinChannelOptions with a temporary token from Agora Console.

var options = new JoinChannelOptions();
options.token = "your_token";
options.withLock = true;

var (status,response) = await streamChannel.JoinAsync(options);
if (status.Error)
{
    Debug.Log(string.Format("{0} is failed,The error code is {1},because of: {2}", status.Operation, status.ErrorCode, status.Reason));
}
else
{
    Debug.Log(string.Format("User:{0} Join stream channel success! at Channel:{1}", response.UserId, response.ChannelName));
}

In stream channels, message flow is managed using topics. Even if you configure a global message listener, you must still join a topic to send messages. Similarly, to receive messages you must subscribe to a topic. See Topics for more information.

Send a message

To send a message to a stream channel:

  • Create an IStreamChannel instance.
  • Use the JoinAsync method to join a channel.
  • Call JoinTopicAsync to register as a message publisher for the specified topic. See Topics.

Call PublishTopicMessageAsync to publish a message to a topic. This method sends a message to a single topic at a time. To deliver messages to multiple topics, call the method separately for each topic.

Refer to the following sample code for sending messages:

  • String message

    // Send a string message
    string message = "left"; // Message content
    string topic = "Motion"; // Topic to which the message is sent
    var options = new TopicMessageOptions(); // Options for sending the message
    options.customType = "PlainTxt"; // Set the custom message type to "PlainTxt"
    var (status, response) = await streamChannel.PublishTopicMessageAsync(topic, message, options); // Send the message asynchronously
    if (status.Error)
    {
        Debug.Log(string.Format("{0} is failed, ErrorCode {1}, due to: {2}", status.Operation, status.ErrorCode, status.Reason)); // Log error information if message send fails
    }
  • Binary message

    // Send a binary message
    byte[] message = new byte[] { 00, 01, 35, 196 }; // Binary message content
    string topic = "Motion"; // Topic to which the message is sent
    var options = new TopicMessageOptions(); // Options for sending the message
    options.customType = "ByteArray"; // Set the custom message type to "ByteArray"
    var (status, response) = await streamChannel.PublishTopicMessageAsync(topic, message, options); // Send the message asynchronously
    if (status.Error)
    {
        Debug.Log(string.Format("{0} is failed, ErrorCode: {1}, due to: {2}", status.Operation, status.ErrorCode, status.Reason)); // Log error information if message send fails
    }

In Signaling, a user may register as a message publisher for up to 8 topics concurrently. However, there are no limitations on the number of users a single topic can accommodate. You can achieve a message transmission frequency to a topic of up to 120 QPS. This capability is useful in use-cases that demand high-frequency and high-concurrency data processing, such as Metaverse location status synchronization, collaborative office sketchpad applications, and parallel control operation-instructions transmission.

Leave a stream channel

To leave a channel, call the LeaveAsync method on the IStreamChannel instance:

var (status,response) = await streamChannel.LeaveAsync();
if (status.Error)
{
    Debug.Log(string.Format("{0} is failed, ErrorCode: {1}, due to: {2}", status.Operation, status.ErrorCode, status.Reason));
}
else
{
    Debug.Log(string.Format("User:{0} Join stream channel success! at Channel:{1}", response.UserId, response.ChannelName));
}

To rejoin a stream channel, call the JoinAsync method again. You can join and leave as long as the corresponding IStreamChannel instance remains active and has not been destroyed.

Destroy the stream channel instance

To destroy a stream channel instance, call Dispose:

var status = streamChannel.Dispose();
if (status.Error)
{
    Debug.Log(string.Format("{0} is failed, ErrorCode: {1}, due to: {2}", status.Operation, status.ErrorCode, status.Reason));
}
else
{
    Debug.Log("Dispose Channel Success!");
}

Destroying a stream channel removes the IStreamChannel instance from your app. This action is local to your app and does not affect other users or the Signaling channel.

Reference

This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

Message packet size

Signaling SDK imposes size limits on message payload packets sent in message type channels and stream type channels: 32 KB for message type channels and 1 KB for stream type channels. The message payload packet size includes the message payload itself plus the size of the customType field. If the message payload package size exceeds the limit, you will receive an error message.

// Status
{
    Error = true;
    ErrorCode =  -11010;
    Operation = "PublishTopicMessageOperation";
    Reason = "CHANNEL_MESSAGE_LENGTH_EXCEED_LIMITATION";
}

To avoid sending failure due to message payload packet size exceeding the limit, check the packet size before sending.

API reference