Web

Updated

API reference for the Agora Signaling Web SDK.

The Signaling SDK API reference is divided into the following sections:

Setup

The API reference for the Signaling SDK documents interface descriptions, methods, basic usage, and return values of the Signaling APIs.

Before you initialize a Signaling client instance, import the Signaling JavaScript SDK into your project:

  • Using CDN:
    <script src="your_path_to_signaling_sdk/agora-rtm.x.y.z.min.js"></script>
  • Using a package manager:
    npm install agora-rtm-sdk

Initialization

Description

Initialization in Signaling refers to creating and initializing a Signaling client instance. When initializing the instance, you need to pass in parameters including appId and userId. You can create a project and get the App ID on the Agora console.

Information


- The initialization step needs to be completed before calling the other Signaling APIs.
- In order to identifying users and devices, you need to ensure that userId is globally unique and remains constant throughout the lifecycle of the user or device.

Method

You can create and initialize an instance as follows:

class RTM(
    constructor(
        appId: string,
        userId: string,
        rtmConfig?: {
             encryptionMode: string,
             cipherKey: string,
             salt: Uint8Array,
             useStringUserId: boolean,
             presenceTimeout: number,
             logUpload: boolean,
             logLevel: string,
             cloudProxy: boolean
         }
    );
)
ParameterTypeRequiredDefaultDescription
appIdstringRequired-The App ID of your Agora project on the Agora console.
userIdstringRequired-The unique ID to identify a user or device.
rtmConfigRTMConfigOptional-The configuration parameters for initialization, see RTMConfig.

Basic Usage

const { RTM } = AgoraRTM;
const rtm = new RTM("yourAppId", "Tony");

Return Value

A Signaling client instance. Now you can call other Signaling APIs.

RTMConfig

Description

RTMConfig is used to configure additional properties when you initializing a Signaling client instance. These configuration properties take effect throughout the lifecycle of the Signaling client and affect the behaviors of the Signaling client.

Method

You can create a RTMConfig instance as follows:

const { RTMConfig } = AgoraRTM;
PropertyTypeRequiredDefault描述
encryptionModestringOptional-Encryption mode for end-to-end messages. If you do not set this property or set it as NONE, end-to-end encryption is disabled. For details, see Encryption Mode.
saltUint8ArrayOptional-The salt required for encryption. The value must be a 32-byte binary array.
cipherKeystringOptional-The key used for encryption and decryption. You must set this property if you want to enable message encryption.
presenceTimeoutnumberOptional300Presence timeout in seconds, and the value range is [5,300]. This parameter refers the delay imposed by the Signaling server before sending a REMOTE_TIMEOUT event notification to other users once it determines that a client has timed out. If the client reconnects and returns to the channel within the specified time, the Signaling server does not send the REMOTE_TIMEOUT event notification to other participants or delete the temporary user data associated with the user.
logUploadbooleanOptionalfalseWhether to upload logs to the server:
- true: Enable log upload
- false: Disable log upload.
cloudProxybooleanOptionalfalseWhether to enable the cloud proxy:
- true: Enable.
- false: Disable. Note: This feature applies to the Message channels and User channels only.
useStringUserIdbooleanOptionaltrueWhether to use string-type user IDs:
- true: Use string-type user IDs.
- false: Use number-type user IDs. If you set the property as false, SDK automatically converts string-type user IDs to number-type ones. In this case, the userId parameter must be a numeric string (for example, "123456"), otherwise initialization fails.
logLevelstringOptional-Set the output level of SDK log. For details, see log output level.
heartbeatIntervalnumberOptional5Heartbeat interval in seconds, and the value range is [5,1800]. This parameter refers to the time interval at which the client sends heartbeat packets to the Signaling server. If the client fails to send heartbeat packets to the Signaling server within the specified time, the Signaling server determines that the client has timed out. Please note that this parameter affects the PCU count, which in turn affects billing.
privateConfigobjectOptional-When using the private deployment feature of Signaling, you need to configure this parameter.

privateConfig contains the following properties:

PropertyTypeRequiredDefaultDescription
serviceTypestring[]Optional-Service type. See Service Types.
accessPointHostsstring[]Optional-An addresses list of servers to access the Signaling service. Only supports domain name.
eventUploadHostsstring[]Optional-An addresses list of servers for event uploading. Only supports domain name.
logUploadHostsstring[]Optional-An addresses list of servers for log uploading. Only supports domain name.
originDomainsstring[]Optional-A list of domain suffixes used when connecting to the Signaling service. Only supports domain name.
domainModenumberOptional1Domain name format of the SDK edge node server in private deployments of Signaling. The following values are supported:
- 1 (default): Custom format. For example, ip.**.
- 2: legacy private deployment format. For example, ip.edge.**.

Note

To upgrade the privately deployed Signaling Web SDK to v2.2.4 or later, set domainMode to 2 for compatibility. If you omit this parameter or use the default value 1, the SDK may fail to connect to the Signaling server.

Basic Usage

const { RTM, EncryptionMode } = AgoraRTM;
const rtmConfig = {
    encryptionMode : EncryptionMode.AES_256_GCM,
    salt : yourSalt,
    cipherKey : "yourCipherKey",
    presenceTimeout : 300,
    logUpload : true,
    logLevel : "debug",
    cloudProxy : false,
    useStringUserId : false,
    privateConfig: {
        serviceType: ["MESSAGE", "STREAM"],
        domainMode: 2
    },
    heartbeatInterval: 5
};
const rtm = new RTM("yourAppId", "Tony", rtmConfig);

Event Listeners

Description

Signaling has a total of 8 types of event notifications, as shown in the following table:

Event TypeDescription
messageReceive message event notifications in subscribed message channels and subscribed topics.
presenceReceive presence event notifications in subscribed message channels and joined stream channels.
topicReceive all topic event notifications in joined stream channels.
storageReceive channel metadata event notifications in subscribed message channels and joined stream channels, and the user metadata event notification of the subscribed users.
lockReceive lock event notifications in subscribed message channels and joined stream channels.
status(Deprecated) Receive event notifications when client connection status changes. For details, see SDK connection state and SDK connection state change reason.
linkStateReceive event notifications when client connection status changes. For details, see SDK Link State Types.
tokenPrivilegeWillExpireReceive event notifications when the client tokens are about to expire.

Add event listeners

You can add event listeners as follows:

 // Add message event listeners
 // Message
rtm.addEventListener("message", event => {
    const channelType = event.channelType; // Which channel type it is, Should be "STREAM", "MESSAGE" or "USER".
    const channelName = event.channelName; // Which channel does this message come from
    const topic = event.topicName; // Which Topic does this message come from, it is valid when the channelType is "STREAM".
    const messageType = event.messageType; // Which message type it is, Should be "STRING" or "BINARY" .
    const customType = event.customType; // User defined type
    const publisher = event.publisher; // Message publisher
    const message = event.message; // Message payload
    const timestamp = event.timestamp; // Event timestamp
});
 // Presence
rtm.addEventListener("presence", event => {
    const action = event.eventType; // Which action it is ,should be one of 'SNAPSHOT'、'INTERVAL'、'JOIN'、'LEAVE'、'TIMEOUT、'STATE_CHANGED'、'OUT_OF_SERVICE'.
    const channelType = event.channelType; // Which channel type it is, Should be "STREAM", "MESSAGE" or "USER".
    const channelName = event.channelName; // Which channel does this event come from
    const publisher = event.publisher; // Who trigger this event
    const states = event.stateChanged; // User state payload, only for stateChanged event
    const interval = event.interval; // Interval payload, only for interval event
    const snapshot = event.snapshot; // Snapshot payload, only for snapshot event
    const timestamp = event.timestamp; // Event timestamp
});
 // Topic
rtm.addEventListener("topic", event => {
    const action = event.evenType; // Which action it is ,should be one of 'SNAPSHOT'、'JOIN'、'LEAVE'.
    const channelName = event.channelName; // Which channel does this event come from
    const publisher = event.userId; // Who trigger this event
    const topicInfos = event.topicInfos; // Topic information payload
    const totalTopics = event.totalTopics; // How many topics
    const timestamp = event.timestamp; // Event timestamp
});
 // Storage
rtm.addEventListener("storage", event => {
    const channelType = event.channelType; // Which channel type it is, Should be "STREAM", "MESSAGE" or "USER".
    const channelName = event.channelName; // Which channel does this event come from
    const publisher = event.publisher; // Who trigger this event
    const storageType = event.storageType; // Which category the event is, should be 'USER'、'CHANNEL'
    const action = event.eventType; // Which action it is ,should be one of "SNAPSHOT"、"SET"、"REMOVE"、"UPDATE" or "NONE"
    const data = event.data; // 'USER_METADATA' or 'CHANNEL_METADATA' payload
    const timestamp = event.timestamp; // Event timestamp
});
 // Lock
rtm.addEventListener("lock", event => {
    const channelType = event.channelType; // Which channel type it is, Should be "STREAM", "MESSAGE" or "USER".
    const channelName = event.channelName; // Which channel does this event come from
    const publisher = event.publisher; // Who trigger this event
    const action = event.evenType; // Which action it is ,should be one of 'SET'、'REMOVED'、'ACQUIRED'、'RELEASED'、'EXPIRED'、'SNAPSHOT'
    const lockName = event.lockName; // Which lock it effect
    const ttl = event.ttl; // The ttl of this lock
    const snapshot = event.snapshot; // Snapshot payload
    const owner = event.owner; // The owner of this lock
    const timestamp = event.timestamp; // Event timestamp
});
 // Connection State Change
rtm.addEventListener("status", event => {
    const currentState = event.state; // Which connection state right now
    const changeReason = event.reason; // Why trigger this event
    const timestamp = event.timestamp; // Event timestamp
});
 // Link State Change
rtm.addEventListener('linkState', event => {
    const currentState = event.currentState;
    const previousState = event.previousState;
    const serviceType = event.serviceType;
    const operation = event.operation;
    const reason = event.reason;
    const affectedChannels = event.affectedChannels;
    const unrestoredChannels = event.unrestoredChannels;
    const timestamp = event.timestamp;
    const isResumed = event.isResumed;
 // Token Privilege Will Expire
rtm.addEventListener("tokenPrivilegeWillExpire", (channelName) => {
    const channelName = channelName; // Which Channel Token Will Expire
});

Remove event listeners

You can call the config. removedelegate[javascript] method to remove a specified event listener.

rtm.removeEventListener("status", statusHandler);

login

Description

After creating and initializing a Signaling client instance, you need to perform the login operation to log in to the Signaling service. With successful login, the client establishes a long link to the Signaling server and allow the client to access Signaling resources.

Method

You can call the login method as follows:

rtm.login(options?: object): Promise<LoginResponse>;
ParameterTypeRequiredDefaultDescription
optionsobjectOptional-Options for logging into a channel.

The options object includes the following properties:

PropertyTypeRequiredDefaultDescription
tokenstringOptional-The token used for logging to the Signaling system.
- If your project enables token authentication, you can provide either the Signaling temporary token or the Signaling token generated by your token server. See User authentication and Deploy Signaling token generator.
- If your project does not enable token authentication, you can enter an empty string or the App ID of a project that enables Signaling services.

Basic Usage

try{
    const result = await rtm.login({ token: "your_token" });
    console.log(result);
} catch (status){
    console.log(status);
}

Return Value

If the method call succeeds, the LoginResponse response as follows is returned:

type LoginResponse = {
    timestamp: number // Reserved property, indicating the timestamp of the successful operation
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

logout

Description

You can log out of the Signaling system if you don't need to perform any operation.

Method

You can call the logout method as follows:

rtm.logout(): Promise<LogoutResponse>;

Basic Usage

try{
    const result = await rtm.logout();
    console.log(result);
} catch (status){
    console.log(status);
}

Return Value

If the method call succeeds, the LogoutResponse response as follows is returned:

type LoginResponse = {
    timestamp: number // Reserved property, indicating the timestamp of the successful operation
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

User authentication

Authentication is the process of validating the identity of each user before they access a system. Agora uses digital tokens to authenticate users and their privileges.

Signaling provides 3 types of channels: message channels, user channels and stream channels. Different types of channels require different types of tokens:

  • For message channels and user channels: When logging in to the Signaling system using the login method, you only need to pass the token that enables Signaling service.
  • For stream channels: In addition to the Signaling token, when joining a stream channel using the joinTopic method, you also need to pass the token that enables RTC service.

The token is valid for up to 24 hours. Agora recommends that you update the token before it expires. This article describes how to update the token.

For more information on generating and using tokens, see the following guide:

renewToken

Description

Call the renewToken method to renew the RTC token.

Different parameter settings applies to different token types:

  • RTM Token: Only need to fill in the token parameter.
  • RTC Token: Need to fill in both the token and channelName parameters.

To ensure timely token updates, Agora recommends listening for the tokenPrivilegeWillExpire callback. See Event listeners for details. Once you successfully add the event listener, when the RTC token is about to expire within 30 seconds, the SDK triggers the tokenPrivilegeWillExpire callback to notify the user about the impending token expiration.

Upon receiving this callback, you can generate a new token on the server-side and call the renewToken method to provide the SDK with the newly generated token.

Method

You can call the renewToken method as follows:

rtm.renewToken(token: string, options?: object): Promise<RenewTokenResponse>
ParametersTypeRequiredDefaultDescription
tokenstringRequired-The newly generated Signaling token.
optionsobjectOptional-Token options.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
channelNamestringOptional-Channel name. For RTC token, this property is required.

Basic usage

Example 1: Renew the Signaling token

rtmClient.addEventListener('tokenPrivilegeWillExpire', async (channelName) => {
    if(!channelName){
        // The RTM Token is about to expire
        const newToken = "<Your new token>";
        await rtmClient.renewToken(newToken);
    }
});

Example 2: Renew the RTC token

rtmClient.addEventListener('tokenPrivilegeWillExpire', async (streamChannelName) => {
    if(streamChannelName){
        // The RTC Token is about to expire
        const newToken = "<Your new token>";
        await rtmClient.renewToken(newToken, {
            channelName: streamChannelName
        });
    }
});

Return value

If the method call succeeds, the RenewTokenResponse response as follows is returned:

type RenewTokenResponse = {
    timestamp: number , // Timestamp of the successful operation.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Channels

Signaling provides a highly efficient channel management mechanism for data transmission. Any user who subscribes or joins a channel can receive messages and events transmitted within 100 milliseconds. Signaling allows clients to subscribe to hundreds or even thousands of channels. Most Signaling APIs perform actions such as sending, receiving, and encrypting based on channels.

Based on capabilities of Agora, Signaling channels are divided into two types to match different application use-cases:

  • Message Channel: Follows the industry-standard Pub/Sub (publish/subscribe) mode. You can send and receive messages within the channel by subscribing to a channel, and do not need to create the channel in advance. There is no limit to the number of publishers and subscribers in a channel.

  • Stream Channel: Follows a concept similar to the observer pattern in the industry, where users need to create and join a channel before sending and receiving messages. You can create different topics in the channel, and messages are organized and managed through topics.

subscribeTopic

Description

Signaling provides event notification capabilities for messages and states. By listening for callbacks, you can receive messages and events within subscribed channels. For information on how to add and set up event listeners, see Event Listener.

By calling the subscribeTopic method, the client can subscribe to a message channel and start receiving messages and event notifications within the channel. After successfully calling this method, users who subscribe to the channel and enable the presence event listener can receive a presence event with the REMOTE_JOIN type.

Information

This method only applies to the message channel.

Method

You can call the subscribeTopic method as follows:

rtm.subscribe(
    channelName: string,
    options?: object
): Promise<SubscribeResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringYes-The channel name.
optionsobjectOptional-Options for subscribing a channel.

The options object includes the following properties:

PropertyTypeRequiredDefaultDescription
withMessagebooleanOptionaltrueWhether to subscribe to message event notifications in the channel.
withPresencebooleanOptionaltrueWhether to subscribe to presence event notifications in the channel.
beQuietbooleanOptionalfalseWhether to set the silent mode. If you set this parameter as true, the SDK has the following behaviors:
- You can still receive other users' event notifications.
- Event notifications related to your channel activity such as subscribing or unsubscribing the channel, and actions related to setting, getting, or deleting temporary user states, can not be broadcasted to other users.
- When calling the getOnlineUsers method, your information can not be found.
- When calling the getUserChannels method, channels that you subscribe in silent mode can not be detected.
withMetadatabooleanOptionalfalseWhether to subscribe to storage event notifications in the channel.
withLockbooleanOptionalfalseWhether to subscribe to lock event notifications in the channel.

Basic usage

const options ={
    withMessage : true,
    withPresence : true,
    beQuiet : false,
    withMetadata : false,
    withLock : false
};
try {
    const result = await rtm.subscribe("chat_room", options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the SubscribeTopicResponse response as follows is returned:

type SubscribeResponse = {
    timestamp : number // Timestamp of the successful operation.
    channelName : string // Channel name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

unsubscribeTopic

Description

If you no longer need to subscribe to a channel, you can call the unsubscribeTopic method to cancel your subscription. After successfully unsubscribing from the channel, other users who subscribe to the channel and enable event listeners can receive a presence event notification with the REMOTE_LEAVE type. For details, see Event Listener.

Information

This method only applies to the message channel.

Method

You can call the unsubscribeTopic method as follows:

rtm.unsubscribe(
    channelName: string
): Promise<UnsubscribeResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringYes-The channel name.

Basic usage

try {
    const result = await rtm.unsubscribe("chat_room");
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the UnsubscribeTopicResponse response as follows is returned:

type UnsubscribeResponse = {
    timestamp : number // Timestamp of the successful operation.
    channelName : string // Channel name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

createAgoraRtmClient

Description

Before using a stream channel, you need to call the createAgoraRtmClient method to create an RTMStreamChannel instance. After successfully creating the instance, you can call its relevant methods to implement functions, such as joining the channel, leaving the channel, sending messages in a topic, and subscribing to messages in a topic.

Information

This method only applies to the stream channel.

Method

You can call the createAgoraRtmClient method as follows:

rtm.createStreamChannel(chanelName: string): RTMStreamChannel;
ParameterTypeRequiredDefaultDescription
channelNamestringYes-The channel name.

Basic usage

try{
    const Loc_stChannel = await rtm.createStreamChannel( "Location");
    console.log("Create Stream Channel success!: ");
} catch (status){
    console.log(status);
}

Return value

An RTMStreamChannel instance.

joinTopic

Description

After successfully creating a stream channel, you can call the joinTopic method to join the stream channel. Once you join the channel, you can implement channel-related functions. At this point, users who subscribe to the channel and add event listeners can receive the following event notifications:

  • Local users:
    • presence event notification with the SNAPSHOT type.
    • topicevent notification with the SNAPSHOT type.
  • Remote users: presence event notification with the REMOTE_JOIN type.

Information

This method only applies to the stream channel.

Method

You can call the joinTopic method as follows:

join(options?: object): Promise<JoinChannelResponse>;
ParameterTypeRequiredDefaultDescription
optionsobjectOptional-Options for joining a channel.

The options object includes the following properties:

PropertyTypeRequiredDefaultDescription
tokenstringOptional-The token used for joining a stream channel, which is currently the same as the RTC token.
withPresencebooleanOptionaltrueWhether to subscribe to presence event notifications in the channel.
beQuietbooleanOptionalfalseWhether to set the silent mode. If you set this parameter as true, the SDK has the following behaviors:
- You can still receive other users' event notifications.
- Event notifications related to your channel activity such as joining or leaving the channel, and actions related to setting, getting, or deleting temporary user states, can not be broadcasted to other users.
- When calling the getOnlineUsers method, your information can not be found.
- When calling the getUserChannels method, channels that you subscribe in silent mode can not be detected.
withMetadatabooleanOptionalfalseWhether to subscribe to storage event notifications in the channel.
withLockbooleanOptionalfalseWhether to subscribe to lock event notifications in the channel.

Basic usage

const options ={
    token : "yourToken",
    withPresence : true,
    beQuiet : false,
    withMetadata : false,
    withLock : false
};
try {
    const result = await stChannel.join(options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the JoinTopicResponse response as follows is returned:

type JoinChannelResponse = {
    timestamp : number , // Timestamp of the successful operation.
    channelName : string // Channel name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

leaveTopic

Description

If you no longer need to stay in a channel, you can call the leaveTopic method to leave the channel. After leaving the channel, you can no longer receive any messages, states, or event notifications from this channel. At the same time, you can no loger be the topic publisher or subscriber of all topics. If you want to restore your previous publisher role and subscribing relationship, you need to call joinTopic, joinTopic and subscribeTopic methods in order.

After successfully leaving the channel, remote users in the channel can receive a presence event notification with the REMOTE_LEAVE type. For details, see Event Listener.

Information

This method only applies to the stream channel.

Method

You can call the leaveTopic method as follows:

leave(): Promise<LeaveChannelResponse>;

Basic usage

try{
    const result = await streamChannel.leave();
    console.log(result);
} catch (status){
    console.log(status);
}

Return value

If the method call succeeds, the LeaveTopicResponse response as follows is returned:

type LeaveChannelResponse = {
    timestamp : number // Timestamp of the successful operation.
    channelName : string // Channel name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Topics

Topic is a data stream management mechanism in stream channels. Users can use topics to subscribe to and distribute data streams, as well as notify events in data streams in stream channels.

Information

Topics only exist in stream channels. Therefore, before using relevant features, users need to create an RTMStreamChannel instance.

For more information on the features of topic, click the following card:

joinTopic

Description

The purpose of joining a topic is to register as one of the message publishers for the topic, so that the user can send messages in the topic. This operation does not affect whether or not the user becomes a subscriber to the topic.

Information

  • Currently, Signaling supports a single client joining up to 8 topics in the same stream channel at a time.
  • Before joining a topic, a user need to create an RTMStreamChannel instance and call the joinTopic method to join the stream channel.

After successfully joining a topic, users who subscribe to that topic and add event listeners can receive the topic event notification with the REMOTE_JOIN type. For details, see Event Listener.

Method

You can call the joinTopic method as follows:

joinTopic(
    topicName: string,
    options?: object
): Promise<JoinTopicResponse>;
ParameterTypeRequiredDefaultDescription
topicNamestringYes-The topic name.
optionsobjectOptional-The reserved property.

Basic usage

try {
    const result = await stChannel.joinTopic( "gesture", options);
    console.log( result);
} catch (status) {
     console.log(status);
}

Return value

If the method call succeeds, the JoinTopicResponse response as follows is returned:

type JoinTopicResponse = {
    timestamp: number , // Timestamp of the successful operation.
    topicName: string // Topic name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

publishTopicMessage

Description

Call the publishTopicMessage method to send messages to a topic. Users who subscribe to this topic and the message publisher in the channel can receive the message within 100 ms. Before calling the publishTopicMessage method, users need to join the stream channel, and then register as a message publisher for that topic by calling the joinTopic method.

The messages sent by users are encrypted with TLS during transmission, and data link encryption is enabled by default and cannot be disabled. To achieve a higher level of data security, users can also enable client encryption during initialization. For details, see Setup.

Method

You can call the publishTopicMessage method as follows:

publishTopicMessage(
    topicName: string,
    message: string | Uint8Array,
    options?: object
): Promise<PublishTopicMessageResponse>;
ParameterTypeRequiredDefaultDescription
topicNamestringRequired-The topic name.
messagestring | Uint8ArrayRequired-The message payload. Supports string or Uint8Array type.
optionsobjectOptional-The message options.

The options object includes the following property:

PropertyTypeRequiredDefaultDescription
customTypestringOptional-A user-defined field. Only supports string type.

Basic usage

Example 1: Send string messages to a specified channel.

try {
    const result = await stChannel.publishTopicMessage( "Gesture", JSON.stringify({such: "object"}) );
    console.log(result);
} catch (status) {
    console.log(status);
}

Example 2: Send Uint8Array messages to a specified channel.

const str2ab = function(str) {
    var buf = new ArrayBuffer(str.length * 2); // Each character occupies 2 bytes.
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return buf;
}
var Message=str2ab("hello world")
try {
    const result = await stChannel.publishTopicMessage( "Gesture",Message);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the PublishTopicMessageResponse response as follows is returned:

type PublishTopicMessageResponse = {
    timestamp: number , // Timestamp of the successful operation.
    topicName: string // Topic name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

leaveTopic

Description

To release resources when you no longer need to publish messages to a topic, you can call the leaveTopic method to unregister as a message publisher for that topic. This method does not affect whether or not you subscribe to that topic or any other operations performed by other users on that topic.

After successfully calling this method, users who subscribe to the channel and enable event listeners can receive the topic event notification with the REMOTE_LEAVE type. For details, see Event Listener.

Method

You can call the leaveTopic method as follows:

leaveTopic(topicName: string): Promise<LeaveTopicResponse>;
ParameterTypeRequiredDefaultDescription
topicNamestringRequired-The topic name.

Basic usage

try {
    const result = await stChannel.leaveTopic("gesture");
    console.log(result);
} catch (status) {
     console.log(status);
}

Return value

If the method call succeeds, the LeaveTopicResponse response as follows is returned:

type LeaveTopicResponse = {
    timestamp: number , // Timestamp of the successful operation.
    topicName: string // Topic name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

subscribeTopic

Description

After joining a channel, you can call the subscribeTopic method to subscribe to message publishers of topics in the channel.

subscribeTopic is an incremental method. For example, if you call this method for the first time with a subscribing list of [UserA, UserB], and then call it again with a subscribing list of [UserB, UserC], the final successful subscribing result is [UserA, UserB, UserC].

There is no limit to the number of message publishers that can be registered for a single topic in a channel, but a user can only subscribe to a maximum of 50 topics at the same time in the same channel, and a maximum of 64 message publishers in each topic.

Method

You can call the subscribeTopic method as follows:

subscribeTopic(
    topicName: string,
    options?: object
): Promise<SubscribeTopicResponse>;
ParameterTypeRequiredDefaultDescription
topicNamestringRequired-The topic name.
optionsobjectOptional-The subscribing options.

The options object includes the following property:

PropertyTypeRequiredDefaultDescription
usersstring[]Optional-A list of user IDs of message publishers that you want to subscribe to. If you do not set this property, you can randomly subscribe to up to 64 users by default.

Basic usage

Example 1: Subscribe to a specified message publisher in a topic.

var UIDs = ["zhangsan","lisi","wangwu"]
try {
    const result = await rtm.subscribeTopic( "Gesture", { users:UIDs } );
    console.log(result);
} catch (status) {
    console.log( status);
}

Example 2: Randomly subscribe to 64 message publisher in a topic.

try {
    const result = await stChannel.subscribeTopic("Gesture");
    console.log(result);
}catch(status) {
    console.log(status);
}

Return value

If the method call succeeds, the SubscribeTopicResponse response as follows is returned:

type SubscribeTopicResponse = {
    succeedUsers : string[] , // A list of users who successfully subscribe to the topic.
    failedUsers : string[], // A list of users who fail to subscribe to the topic.
    failedDetails : [ // A list of reasons for subscription failure.
        {
            user : string , // User ID.
            errorCode : number , // Error code.
            reason : string // Reason for the error.
        },
    ],
    timestamp : number, // Timestamp of the successful operation.
    topiclName : string // Topic name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

unsubscribeTopic

Description

If you are no longer interested in a topic or no longer need to subscribe to one or more message publishers in the topic, you can call the unsubscribeTopic method to unsubscribe from the topic or unsubscribe from specific message publishers in the topic.

Method

You can call the unsubscribeTopic method as follows:

unsubscribeTopic(
    topicName: string,
    options?: object
): Promise<UnsubscribeTopicResponse>;
ParameterTypeRequiredDefaultDescription
topicNamestringRequired-The topic name.
optionsobjectOptional-The unsubscribing options.

The options object includes the following property:

PropertyTypeRequiredDefaultDescription
usersstring[]Optional-A list of user IDs of message publishers that you want to unsubscribe from. If you do not set this property, you can unsubscribe from the entire topic.

Basic usage

Example 1: Unsubscribe from a specified message publisher in a topic.

try {
    const result = await rtm.unsubscribeTopic( "Gesture", { users:["Tony","Bo"] });
    console.log("unsubscribe Topic success: ", result);
} catch (status) {
    console.log("unsubscribe Topic failed: ", result);
}

Example 2: Randomly unsubscribe from 64 message publisher in a topic.

try {
    const result = await rtm.unsubscribeTopic("Gesture");
    console.log("unsubscribe topic success: ", result);
} catch (status) {
    console.log("unsubscribe topic failed: ", result);
}

Return value

If the method call succeeds, the UnsubscribeTopicResponse response as follows is returned:

type UnsubscribeTopicResponse = {
    timestamp: number , // Timestamp of the successful operation.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Messages

Sending and receiving messages is the most basic function of the Signaling service. Any message sent by the Signaling server can be delivered to any online subscribing user within 100 ms. Depending on your business requirements, you can send messages to one user only or broadcast messages to multiple users.

Signaling offers 3 types of channels: message channels, user channels, and stream channels. These channel types have the following differences in how messages are transmitted and methods are called:

  • Message Channel: The real-time channel. Messages are transmitted through the channel, and the channel is highly scalable. Local users can call the publishTopicMessage method, set the channelType parameter to MESSAGE, and set the channelName parameter to the channel name to send messages in the channel. The remote users can call the subscribeTopic method to subscribe to the channel and receive messages.
  • User Channel: The real-time channel. Messages are transmitted to the specified user. Local users can call the publishTopicMessage method, set the channelType parameter to USER, and set the channelName parameter to the user ID to send messages to the specified user. The specified remote users receive messages through the message event notifications.
  • Stream Channel: The streaming transmission channel. Messages are transmitted through the topic. Users need to join a channel first, and then join a topic. Local users can call the publishTopicMessage method to send messages in the topic, and remote users can call the subscribeTopic method to subscribe to the topic and receive messages.

This page introduces how to send and receive messages in a message channel or a user channel.

publishTopicMessage

Description

You can directly call the publishTopicMessage method to send messages to all online users who subscribe to the channel. Even if you do not subscribe to the channel, you can still send messages in the channel.

Information

The following practices can effectively improve the reliability of message transmission:
- The message payload should be within 32 KB; otherwise, the sending will fail.
- The upper limit of the rate at which messages are sent to a single channel is 60 QPS. If the sending rate exceeds the limit, some messages will be discarded. A lower rate is better, as long as the requirements are met.

After successfully calling this method, the SDK triggers a message event notification. Users who subscribe to the channel and enabled the event listener can receive this event notification. For details, see Event Listener.

Method

You can call the publishTopicMessage method as follows:

rtm.publish(
    channelName: string,
    message: string | Uint8Array,
    options?: object
): Promise<PublishResponse>;
ParameterTypeRequiredDefaultDescription
messagestring | Uint8ArrayRequired-The message payload. Supports string or Uint8Array type.
channelNamestringRequired-Fill in a channel name to send messages in a specified channel, or fill in a user ID to send messages to a specified user.
optionsobjectOptional-The message options.

The options object includes the following property:

PropertyTypeRequiredDefaultDescription
customTypestringOptional-A user-defined field. Only supports string type.
channelTypestringOptional-Channel type. For details, see Channel Types.

Basic usage

Example 1: Send string messages to a specified channel.

try {
    const result = await rtm.publish( "my_channel", "Hello world" );
    console.log(result);
} catch (status) {
    console.log(status);
}

Example 2: Send Uint8Array messages to a specified channel.

const str2ab = function(str) {
    var buf = new ArrayBuffer(str.length * 2); // Each character occupies 2 bytes.
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return buf;
};
var Message=str2ab("hello world");
try {
    const result = await rtm.publish("my_channel", Message );
    console.log(result);
} catch (status) {
    console.log(status);
}

Example 3: Send string messages to a specified user.

try {
    const result = await rtm.publish("user_b", "Hello world", { channelType: "USER"} );
    console.log(result);
} catch (status) {
    console.log(status);
}

Example 4: Send Uint8Array messages to a specified user.

const str2ab = function(str) {
    var buf = new ArrayBuffer(str.length * 2); // 每个字符占用 2 个字节
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return buf;
};
var Message=str2ab("hello world");
try {
    const result = await rtm.publish("user_b", Message, { channelType: "USER"} );
    console.log(result);
} catch (status) {
    console.log(status);
}

Information

After successfully calling this method, the SDK triggers a message event notification. Users who subscribe to the channel and enable event listener can receive this event notification. For details, see Event Listener.

Return value

If the method call succeeds, the PublishTopicMessageResponse response as follows is returned:

type PublishResponse = {
    timestamp: number , // Timestamp of the successful operation.
    chanelName : string // Channel name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Receive

Signaling provides event notifications for messages, states, and event changes. By listening for callbacks, you can receive messages and events within subscribed channels. As an example, the code snippet below shows how to receive messages from the user channel.

rtm.addEventListener("message", event => {
    const channelType = event.channelType; // Which channel type it is, Should be "STREAM", "MESSAGE" or "USER" .
    const channelName = event.channelName; // Which channel does this message come from
    const topic = event.topicName; // Which Topic does this message come from, it is valid when the channelType is "STREAM".
    const messageType = event.messageType; // Which message type it is, Should be "STRING" or "BINARY" .
    const customType = event.customType; // User defined type
    const publisher = event.publisher; // Message publisher
    const message = event.message; // Message payload
    const timestamp = event.timestamp; // Message timestamp
});

For information on how to add and set event listeners, see Event Listener.

Presence

The presence feature provides the ability to monitor user online, offline, and user historical state change. With the Presence feature, you can get real-time access to the following information:

  • Real-time event notification when a user joins or leaves a specified channel.
  • Real-time event notification when the custom temporary user state changes.
  • Query which channels a specified user has joined or subscribed to.
  • Query which users have joined a specified channel and their temporary user state data.

Information

Presence applies to both message channels and stream channels.

getOnlineUsers

Description

Call the getOnlineUsers method to query user information in real time such as the number of online users, the list of online users and their temporary user status in the specified channel.

Method

You can call the getOnlineUsers method as follows:

rtm.presence.getOnlineUsers(
    channelName: string,
    channelType: string,
    options?: object
): Promise<getOnlineUsersResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
optionsobjectOptional-Query options.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
includedUserIdbooleanOptionaltrueWhether the returned result contains the user ID of online users.
includedStatebooleanOptionalfalseWhether the returned result contains the temporary user state of online users.
pagestringOptional-Page number of the returned result. If you do not provide this property, the SDK returns the first page by default. You can check whether there is next page in the nextPage property of the returned result.

Basic Usage

const options = {
    includedUserId : true ,
    includedState : true,
    page : "yourBookMark"
}
try{
    const result = await rtm.presence.getOnlineUsers( "chat_room", "MESSAGE", options );
    console.log(result);
} catch(status){
    console.log(status);
}

Return Value

If the method call succeeds, the WhoNowResponse response as follows is returned:

type getOnlineUsersResponse = {
    timestamp: number , // Timestamp of the successful operation.
    totalOccupancy : number , // Number of the online users in the channel.
    occupants : Array<object> , // List of the online users in the channel and their temporary user state.
    nextPage : string // Page number of the next page.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

getUserChannels

Description

In use-cases such as statistic analytics and debugging, you may need to know all the channels that a specified user has subscribed to or joined. Call the getUserChannels method to get the list of channels where the specified user is in real time.

Method

You can call the getUserChannels method as follows:

rtm.presence.getUserChannels(userId: string): Promise<GetUserChannelsResponse>;
ParameterTypeRequiredDefaultDescription
userIdstringRequired-If you set this parameter to an empty string "", the SDK uses the userId of the local user.

Basic Usage

try{
    const result = await rtm.presence.whereNow( "Tony" );
    console.log(result);
} catch(status){
    console.log(status);
}

Return Value

If the method call succeeds, the WhereNowResponse response as follows is returned:

type GetUserChannelsResponse = {
    timestamp: number , // Timestamp of the successful operation.
    totalChannel : number , // Number of the channels that the user has joined or subscribed to.
    channels : Array<object> // List of the channels including channel name and channel type.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

setState

Description

To meet different requirements in different business use-cases for setting user states, Signaling provides the setState method to customize the temporary user state. Users can add custom statuses such as scores, game status, location, mood, and hosting status for themselves.

The setState method sets temporary user state. It means these user states remains in the channel as long as the user stays subscribed to the channel and stays online, and they disappear when the user leaves the channel or disconnects from Signaling. If you need to restore user states when rejoining a channel or reconnecting, you need to cache the data locally in real time. If you want to permanently save user states, Agora recommends you use the setUserMetadata method of the storage function instead.

If a user modifies the temporary user status, Signaling triggers the presence event notification of the REMOTE_STATE_CHANGED type in real time . You can receive the event by subscribing to the channel and configuring the corresponding property.

Method

Set the temporary user state as follows:

rtm.presence.setState(
    channelName: string,
    channelType: string,
    state: object
): Promise<SetStateResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
stateobjectRequired-A JSON object that consists of key-value pairs. The key must be string type.

Information

The keys in the state property do not support containing [, ], and . characters.

Basic Usage

var newState = { "mood":"pumped", "isTyping":false};

try{
    const result = await rtm.Presence.setState("chat_room", "MESSAGE", newState);
    console.log(result);
} catch(status){
    console.log(status);
}

Return Value

If the method call succeeds, the SetStateResponse response as follows is returned:

type SetStateResponse = {
    timestamp: number , // Timestamp of the successful operation.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

getState

Description

To get the temporary user state of a specified user in the channel, you can use the getState method.

Method

Get the temporary user state as follows:

rtm.presence.getState(
    userId: string,
    channelName: string,
    channelType: string,
): Promise<GetStateResponse>;
ParameterTypeRequiredDefaultDescription
userIdstringRequired-User ID.
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.

Basic Usage

try{
    const result = await rtm.presence.getState("Tony", "chat_room", "MESSAGE" );
    const.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the GetStateResponse response as follows is returned:

type GetStateResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
    states : object , // Key-value pairs of the user states.
    statesCount : number // Numbers of the user states.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

removeState

Description

When a temporary user state is no longer needed, you can call the removeState method to remove it. When the user state is removed, the user who has subscribed to the channel and enabled the presence event monitoring receives the presence notification in the REMOTE_STATE_CHANGED type. For details, see Event Listener.

Method

You can call the removeState method as follows:

rtm.presence.removeState(
    channelName: string,
    channelType: string,
    options?: object
): Promise<RemoveStateResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
optionsobjectOptional-Options for removing the state.

The options object includes the following property:

PropertyTypeRequiredDefaultDescription
statesstring[]Optional-The list of keys of the user states that you want to remove. If you do not provide this parameter, all the user states are removed.

Information

The keys in the states property do not support containing [, ], and . characters.

Basic Usage

const options = {
    states:["mode","Typing"]
}
try{
    const result = await rtm.Presence.removeState("chat_room", "MESSAGE" , options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the RemoveStateResponse response as follows is returned:

type RemoveStateResponse = {
    timestamp: number , // Timestamp of the successful operation.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Storage

The storage feature provides a dynamic database mechanism that allows developers to dynamically set, store, update, and delete data such as channel metadata and user metadata.

setChannelMetadata

Description

The setChannelMetadata method sets metadata for a message channel or stream channel. A channel can only have one set of metadata, but each set of metadata can have one or more metadata items. If you call this method multiple times, the SDK retrieves the key of the metadata items in turn and apply settings according to the following rules:

  • If you set metadata with different key, the SDK adds each set of metadata in sequence according to the order of the method calls.
  • If you set metadata with the same key, the value of the last setting overwrites the previous one.

After successfully setting channel metadata, users who subscribe to the channel and enable event listeners can receive the storage event notification with the CHANNEL type. For details, see Event Listener.

Channel metadata also introduces the version control logic CAS (Compare And Set). This method provides two independent version control fields, and you can set one or more of them according to your actual business use-case:

  • Enable version number verification for the entire set of channel metadata by setting the majorRevision property in the options parameter.
  • Enable version number verification for a single metadata item by setting the revision property in the data parameter.

When setting channel metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, indicating that no CAS verification is performed for this method call. If the channel metadata or metadata item already exists, the latest value overwrites the previous one. If the channel metadata or metadata item does not exist, the SDK creates it.
  • If the revision property is a positive integer, CAS verification is performed for this method call. If the channel metadata or metadata item already exists, the SDK updates the corresponding value after the version number verification succeeds. If the channel metadata or metadata item does not exist, the SDK returns the error code.

Method

You can call the setChannelMetadata method as follows:

rtm.storage.setChannelMetadata(
    channelName: string,
    channelType: string,
    data: Array<object>,
    options?: object
): Promise<SetChannelMetadataResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
optionsobjectOptional-Options for setting metadata.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

Basic usage

const data = [
    {
        key : "Apple",
        value : "100"
        revision : 174298200
    },
    {
        key : "Banana",
        value : "200",
        revision : 174298100
    }
];
const options = {
    majorRevision : 174298270,
    lockName: "lockName",
    addTimeStamp : true,
    addUserId : true
};
try {
    const result = await rtm.storage.setChannelMetadata( "channel_name", "MESSAGE", data, options );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the SetChannelMetadataResponse response as follows is returned:

type SetChannelMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

getChannelMetadata

Description

getChannelMetadata 方法可以获取指定频道的 Metadata。

Method

You can call the getChannelMetadata method as follows:

rtm.storage.getChannelMetadata(
    channelName: string,
    channelType: string
): Promise<GetChannelMetadataResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.

Basic usage

try {
    const result = await rtm.storage.getChannelMetadata("channel_name", "MESSAGE");
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the GetChannelMetadataResponse response as follows is returned:

type GetChannelMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    totalCount : number , // Number of metadata items.
    majorRevision : number , // Version of metadata.
    metadata : Record<string, MetaDataDetail> // JSON object containing metadata item.
}

MetaDataDetail contains the following properties:

type MetaDataDetail = {
    value: string , // Value of a metadata item.
    revision : number , // Version of a metadata item.
    updated : string , // Timestamp of the last update.
    authorUid : string , // User ID of the last editor.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

removeChannelMetadata

Description

Call the removeChannelMetadata method to remove channel metadata or metadata items.

When removing channel metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, indicating that no CAS verification is performed for this method call. If the channel metadata or metadata item already exists, the SDK removes it. If the channel metadata or metadata item does not exist, the SDK returns the error code.
  • If the revision property is a positive integer, CAS verification is performed for this method call. If the channel metadata or metadata item already exists, the SDK removes the corresponding value after the version number verification succeeds. If the channel metadata or metadata item does not exist, the SDK returns the error code.

After successfully removing channel metadata or metadata items, users who subscribe to the channel and enable event listeners can receive the storage event notification with the CHANNEL type. For details, see Event Listener.

Method

You can call the removeChannelMetadata method as follows:

rtm.storage.removeChannelMetadata(
    channelName: string,
    channelType: string,
    options?: object
): Promise<RemoveChannelMetadataResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
optionsobjectOptional-Options for setting metadata.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

Basic usage

const data = [
    {
        key : "Apple",
        revision : 174298200
    }
];
const options = {
    data : data,
    majorRevision : 174298270,
};
try {
    const result = await rtm.storage.removeChannelMetadata("channel_name", "MESSAGE", options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the RemoveChannelMetadataResponse response as follows is returned:

type RemoveChannelMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

updateChannelMetadata

Description

The updateChannelMetadata method can update existing channel metadata. Each time you call this method, you can update one channel metadata or an array of channel metadata items.

After successfully updating, users who subscribe to the channel and enable event listeners can receive the storage event notification with the CHANNEL type. For details, see Event Listener.

Information

You cannot use this method to update metadata items which do not exist.

Method

You can call the updateChannelMetadata method as follows:

rtm.storage.updateChannelMetadata(
    channelName: string,
    channelType: string,
    data: Array<object>,
    options?: object
): Promise<UpdateChannelMetadataResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
optionsobjectOptional-Options for setting metadata.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

Basic usage

const data = [
    {
        key : "Apple",
        value : "120",
        revision : 174298200
    },
    {
        key : "Banana",
        value : "220",
        revision : 174298100
    }
];
const options = {
    majorRevision : 174298270,
    addTimeStamp : true,
    addUserId : true
};
try {
    const result = await rtm.storage.updateChannelMetadata("channel_name", "MESSAGE", data, options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the UpdateChannelMetadataResponse response as follows is returned:

type UpdateChannelMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

setUserMetadata

Description

The setUserMetadata method sets metadata for a user. A user can only have one set of metadata, but each set of metadata can have one or more metadata items. If you call this method multiple times, the SDK retrieves the key of the metadata items in turn and apply settings according to the following rules:

  • If you set metadata with different key, the SDK adds each set of metadata in sequence according to the order of the method calls.
  • If you set metadata with the same key, the value of the last setting overwrites the previous one.

After successfully setting user metadata, users who subscribe to the user metadata and enable event listeners can receive the storage event notification with the USER type. For details, see Event Listener.

User metadata also introduces the version control logic CAS (Compare And Set). This method provides two independent version control fields, and you can set one or more of them according to your actual business use-case:

  • Enable version number verification for the entire set of user metadata by setting the majorRevision property in the options parameter.
  • Enable version number verification for a single metadata item by setting the revision property in the data parameter.

When setting user metadata or metadata items, you can control whether to enable version number verification by specifying the revision property:

  • The default value of the revision property is -1, indicating that no CAS verification is performed for this method call. If the user metadata or metadata item already exists, the latest value overwrites the previous one. If the user metadata or metadata item does not exist, the SDK creates it.
  • If the revision property is a positive integer, CAS verification is performed for this method call. If the user metadata or metadata item already exists, the SDK updates the corresponding value after the version number verification succeeds. If the user metadata or metadata item does not exist, the SDK returns the error code.

Method

You can call the setUserMetadata method as follows:

rtm.storage.setUserMetadata(
    data: Array<object>,
    options?: object
): Promise<SetUserMetadataResponse>;
ParameterTypeRequiredDefaultDescription
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
optionsobjectOptional-Options for setting metadata.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

Basic usage

const data = [
    {
        key : "Name",
        value : "Tony"
        revision : 174298200
    },
    {
        key : "Mute",
        value : "true",
        revision : 174298100
    }
];
const options = {
    userId : "Tony",
    majorRevision : 174298270,
    addTimeStamp : ture,
    addUserId : true
};
try {
    const result = await rtm.storage.setUserMetadata(data, options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the SetUserMetadataResponse response as follows is returned:

type SetUserMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

getUserMetadata

Description

getUserMetadata 方法可以获取指定用户的 Metadata 和 User Metadata Item。

Method

You can call the getUserMetadata method as follows:

rtm.storage.getUserMetadata(options?: object): Promise<GetUserMetadataResponse>;
ParameterTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.

Basic usage

try {
    const result = await rtm.storage.getUserMetadata({ userId: "Tony" });
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the GetChannelMetadataResponse response as follows is returned:

type GetChannelMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
    totalCount : number , // Number of metadata items.
    majorRevision : number , // Version of metadata.
    metadata : Record<string, MetaDataDetail> // JSON object containing metadata item.
}

MetaDataDetail contains the following properties:

type MetaDataDetail = {
    value: string , // Value of a metadata item.
    revision : number , // Version of a metadata item.
    updated : string , // Timestamp of the last update.
    authorUid : string , // User ID of the last editor.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

removeUserMetadata

Description

Call the removeUserMetadata method to remove user metadata or metadata items.

After successfully removing user metadata or metadata items, users who subscribe to the user metadata and enable event listeners can receive the storage event notification with the USER type. For details, see Event Listener.

Method

You can call the removeUserMetadata method as follows:

rtm.storage.removeUserMetadata(
    options?: object
): Promise<RemoveUserMetadataResponse>;
ParameterTypeRequiredDefaultDescription
optionsobjectOptional-Options for setting metadata.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

Basic usage

const data = [
    {
        key : "Mute",
        revision : 174298100
    }
];
const options = {
    userId: "Tony",
    data : data,
    majorRevision : 174298270
};
try {
    const result = await rtm.storage.removeUserMetadata(options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the RemoveUserMetadataResponse response as follows is returned:

type RemoveUserMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

updateUserMetadata

Description

The updateUserMetadata method can update existing user metadata. Each time you call this method, you can update one user metadata or an array of user metadata items.

After successfully updating, users who subscribe to the user metadata and enable event listeners can receive the storage event notification with the USER type. For details, see Event Listener.

Information

You cannot use this method to update metadata items which do not exist.

Method

You can call the updateUserMetadata method as follows:

rtm.storage.updateUserMetadata(
    data: Array<object>,
    options?: object
): Promise<UpdateUserMetadataResponse>;
ParameterTypeRequiredDefaultDescription
dataArray&lt;object&gt;Required-An array of metadata items. A metadata item is a JSON object that includes predefined properties. You cannot set properties other than predefined ones in this object.
optionsobjectOptional-Options for setting metadata.

data contains the following properties:

PropertyTypeRequiredDefaultDescription
keystringRequired-The key of the metadata item.
valuestringOptionalEmpty string ''The value of the metadata item.
revisionnumberOptional-1
- Returns the real version number in read operations.
- Serves as a version control switch in write operations:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.
majorRevisionnumberOptional-1A version control switch:
- -1: Disable the version verification.
- > 0: Enable the version verification. Only operations that match the target version number can be performed.
lockNamestringOptionalEmpty string ''The name of the lock. If set, only users who call the acquireLock method to acquire the lock can perform operations.
addTimeStampbooleanOptionalfalseWhether to record the timestamp of the edit.
addUserIdbooleanOptionalfalseWhether to record the ID of the editor.

Basic usage

const data = [
    {
        key : "Mute",
        value : "false",
        revision : 174298100
    }
];
const options = {
    userId : "Tony",
    majorRevision : 174298270,
    addTimeStamp : true,
    addUserId : true
};
try {
    const result = await rtm.storage.updateUserMetadata(data, options);
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the UpdateUserMetadataResponse response as follows is returned:

type UpdateUserMetadataResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
    totalCount : number // Number of metadata items.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

subscribeUserMetadata

Description

The subscribeUserMetadata method can subscribe to metadata for a specified user. After successfully subscribing and enabling event listeners, you can receive the storage event notification with the USER type when the metadata for that user changes. For details, see Event Listener.

Method

You can call the subscribeUserMetadata method as follows:

rtm.storage.subscribeUserMetadata(userId: string): Promise<SubscribeUserMetaResponse>;
ParameterTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.

Basic usage

try {
    const result = await rtm.storage.subscribeUserMetadata("Tony");
    console.log(result);
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the SubscribeUserMetaResponse response as follows is returned:

type SubscribeUserMetaResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
}

unsubscribeUserMetadata

Description

If you do not need to receive notifications of changes to a user metadata, call the unsubscribeUserMetadata method to unsubscribe.

Method

You can call the unsubscribeUserMetadata method as follows:

rtm.storage.unsubscribeUserMetadata(userId: string): Promise<UnsubscribeUserMetaResponse>;
ParameterTypeRequiredDefaultDescription
userIdstringOptionalThe userId of the current user.The user ID.

Basic usage

try {
    const result = await rtm.storage.unsubscribeUserMetadata("Tony");
    console.log("result");
} catch (status) {
    console.log(status);
}

Return value

If the method call succeeds, the UnsubscribeUserMetaResponse response as follows is returned:

type UnsubscribeUserMetaResponse = {
    timestamp: number , // Timestamp of the successful operation.
    userId : string , // User ID.
}

Lock

A critical resource can only be used by one process at a time. If a critical resource is shared between different processes, each process needs to adopt a mutually exclusive method to prevent mutual interference. Signaling provides a full set of lock solutions. By controlling different processes in a distributed system, you can solve the competition problem when users access shared resources.

Prompt

The client is able to set, remove, and revoke locks. We recommend that you control the permissions of the these operations on the client side based on your business needs.

setLock

Description

You need to configure the lock name, time to live (TTL) and other parameters by calling the setLock method. If the configuration succeeds, all users in the channel receives the lock event notifications of the SET type. For details, see Event Listeners.

Method

You can call the setLock method as follows:

rtm.lock.setLock(
    channelName: string,
    channelType: string,
    lockName: string,
    options?: object
): Promise<SetLockResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
lockNamestringRequired-Lock name.
optionsobjectOptional-Options for setting locks.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
ttlnumberOptional10Time to live in seconds of the lock, and the value range is [10,300]. When the user who owns the lock goes offline, if the user returns to the channel within the time they can still use the lock; otherwise, the lock is released and the users who listen for the lock event receives the RELEASED event.

Basic Usage

try{
    const result = await rtm.Lock.setLock(
        channel:"my_channel",
        channelType:"STREAM",
        lockName: "my_lock",
        { ttl: 30 }
    );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the SetLockResponse response as follows is returned:

type SetLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    lockName : string // Lock name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

acquireLock

Description

After setting a lock, you can call the acquireLock method on the client to acquire the right to own the lock. When you acquire the lock, other users in the channel receives the lock event of the ACQUIRED type. For details, see Event Listener.

Method

You can call the acquireLock method as follows:

rtm.lock.acquireLock(
    channelName: string,
    channelType: string,
    lockName: string,
    options?: object
): Promise<AcquireLockResponse>;
ParameterTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
lockNamestringRequired-Lock name.
optionsobjectOptional-Options for setting locks.

options contains the following properties:

PropertyTypeRequiredDefaultDescription
retrybooleanOptionalfalseIf the lock acquisition fails, whether to retry until the acquisition succeeds or the user leaves the channel.

Basic Usage

try{
    const result = await rtm.Lock.acquireLock(
        "chat_room",
        "STREAM",
        "my_lock",
        {retry:false}
    );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the AcquireLockResponse response as follows is returned:

type AcquireLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    lockName : string // Lock name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

releaseLock

Description

When a user no longer needs to own a lock, the user can call the releaseLock method on the client side to release the lock. When the lock is released, other users in the channel receives the lock event of RELEASED type. For details, see Event Listeners.

At this time, if other users want to acquire the lock, they can call the acquireLock method on the client side to compete. New users acquiring locks have the same contention priority as the users who set the retry property to automatically retry to acquire locks.

Method

You can call the acquireLock method as follows:

rtm.lock.releaseLock(
    channelName: string,
    channelType: string,
    lockName: string
): Promise<ReleaseLockResponse>;
PropertyTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
lockNamestringRequired-Lock name.

Basic Usage

try{
    const result = await rtm.lock.releaseLock(
        "chat_room",
        "STREAM",
        "my_lock"
    );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the ReleaseLockResponse response as follows is returned:

type ReleaseLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    lockName : string // Lock name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

revokeLock

Description

You may need to take back a lock owned by another user and allow the other users to acquire the lock. You can call the revokeLock method to revoke the lock. When the lock is revoked, all users in the channel receives the lock event of the RELEASED type. For details, see Event Listeners.

At this time, if other users want to acquire the lock, they can call the acquireLock method on the client side to compete. New users acquiring locks have the same contention priority as the users who set the retry property to automatically retry to acquire locks.

Method

You can call the revokeLock method as follows:

rtm.lock.revokeLock(
    channelName: string,
    channelType: string,
    lockName: string,
    owner: string
): Promise<RevokeLockResponse>;
PropertyTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
lockNamestringRequired-Lock name.
ownerstringRequired-User ID of the lock owner.

Basic Usage

try{
    const result = await rtm.lock.revokeLockLock(
        "chat_room",
        "STREAM",
        "my_lock",
        "Tony"
    );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the RevokeLockResponse response as follows is returned:

type RevokeLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    lockName : string // Lock name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

getMessages

Description

If you want to query the lock information such as lock total number, lock name, and lock user, time to live, you can call the getMessages method on the client.

Method

You can call the getMessages method as follows:

rtm.lock.getLock(
    channelName: string,
    channelType: string
): Promise<GetLockResponse>;
PropertyTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.

Basic Usage

try{
    const result = await rtm.lock.getLock("chat_room","STREAM");
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the GetLockResponse response as follows is returned:

type GetLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    totalLocks : string, // Total lock number.
    lockDetails : [{
         lockName: string, // Lock name.
         owner: string, // Lock owner.
         ttl: number // Time to live in seconds of the lock.
     }]
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

removeLock

Description

If you no longer need a lock, you can remove the lock by calling the removeLock method. When the lock is successfully removed, all the users in the channel receives the lock event notification in the REMOVED type. For details, see Event Handlers.

Method

You can call the removeLock method as follows:

rtm.lock.removeLock(
    channelName: string,
    channelType: string,
    lockName: string
): Promise<RemoveLockResponse>;
PropertyTypeRequiredDefaultDescription
channelNamestringRequired-Channel name.
channelTypestringRequired-Channel type. For details, see Channel Types.
lockNamestringRequired-Lock name.

Basic Usage

try{
    const result = await rtm.Lock.removeLockLock(
        "chat_room",
        "STREAM",
        "my_lock"
    );
    console.log(result);
} catch (status) {
    console.log(status);
}

Return Value

If the method call succeeds, the RemoveLockResponse response as follows is returned:

type RemoveLockResponse = {
    timestamp: number , // Timestamp of the successful operation.
    channelName : string , // Channel name.
    channelType : string , // Channel type.
    lockName : string, // Lock name.
}

If the method call fails, the ErrorInfo response as follows is returned:

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Enumerated types

Channel Types

ValueDescription
MESSAGEMessage channel.
STREAMStream channel.
USERUser channel.
ValueDescription
IDLEThe initial state.
CONNECTINGThe SDK is trying to connect with the Signaling system.
CONNECTEDThe SDK connects with the Signaling system.
DISCONNECTEDThe SDK disconnects with the Signaling system.
SUSPENDEDThe SDK suspends the connecting task.
FAILEDThe SDK fails to connect with the Signaling system.
ValueDescription
UNKNOWNUnknown reason.
LOGINLogging in.
LOGIN_SUCCESSLogin successful.
LOGIN_TIMEOUTLogin timeout.
LOGIN_NOT_AUTHORIZEDLogin not authorized.
LOGIN_REJECTEDLogin rejected.
RELOGINRe-login.
LOGOUTLogout.
AUTO_RECONNECTAuto-reconnect.
RECONNECT_TIMEOUTReconnect timeout.
RECONNECT_SUCCESSReconnect successful.
JOINJoining a channel.
JOIN_SUCCESSJoin channel successful.
JOIN_FAILEDJoin channel failed.
REJOINRe-join a channel.
LEAVELeave a channel.
INVALID_TOKENInvalid token.
TOKEN_EXPIREDToken expired.
INCONSISTENT_APP_IDInconsistent App ID.
INVALID_CHANNEL_NAMEInvalid channel name.
INVALID_USER_IDInvalid user ID.
NOT_INITIALIZEDSDK not initialized.
RTM_SERVICE_NOT_CONNECTEDRTM service not connected.
CHANNEL_INSTANCE_EXCEED_LIMITATIONChannel instance exceeds the limit.
OPERATION_RATE_EXCEED_LIMITATIONOperation rate exceeds the limit.
CHANNEL_IN_ERROR_STATEChannel in error state.
PRESENCE_NOT_CONNECTEDPresence service not connected.
SAME_UID_LOGINSame user ID login.
KICKED_OUT_BY_SERVERKicked out by server.
KEEP_ALIVE_TIMEOUTKeepalive timeout.
CONNECTION_ERRORConnection error.
PRESENCE_NOT_READYPresence service not ready.
NETWORK_CHANGENetwork changed.
SERVICE_NOT_SUPPORTEDService not supported.
STREAM_CHANNEL_NOT_AVAILABLEStream Channel not available.
STORAGE_NOT_AVAILABLEStorage not available.
LOCK_NOT_AVAILABLELock not available.
LOGIN_TOO_FREQUENTThe login operation is too frequent.

(Deprecated) Reasons for the SDK Connection State Change

ValueDescription
CONNECTINGThe SDK is connecting with the Signaling system.
LOGIN_SUCCESSThe SDK logged in the Signaling system successfully.
REJECTED_BY_SERVERThe SDK is rejected by the server.
LOSTThe SDK lost the connection with the Signaling system.
INTERRUPTEDThe connection was interrupted.
LOGOUTThe SDK logged out of the Signaling system.
/The SDK is banned by the server.
SAME_UID_LOGINThe same user ID was used to join the same channel on different devices.
TOKEN_EXPIREDThe used token expired. You need to request a new token from your server.

(Deprecated) SDK Connection States

ValueDescription
DISCONNECTEDThe SDK has disconnected with the server.
CONNECTINGThe SDK is connecting with the server.
CONNECTEDThe SDK has connected with the server.
RECONNECTINGThe connection is lost. The SDK is reconnecting with the server.
FAILEDThe SDK failed to connect with the server.

End-to-End Encryption Modes

ValueDescription
NONENo encryption.
AES_128_GCMAES-128-GCM mode.
AES_256_GCMAES-256-GCM mode.

Lock Event Types

ValueDescription
SNAPSHOTThe snapshot of the lock when the user joined the channel.
SETThe lock is set.
REMOVEDhe lock is removed.
ACQUIREDThe lock is acquired.
RELEASEDThe lock is released.
EXPIREDThe lock expired.

Log Output Levels

ValueDescription
noneNo log.
infoOutput the log at the error, warn, or info level. Agora recommends you set to this value.
warnOutput the log at the error or warn level.
errorOutput the log at the error level.
debugOutput the log at all levels.

Message Types

ValueDescription
BINARYBinary type.
STRINGString type.

Presence Event Types

ValueDescription
SNAPSHOTWhen users joined a channel for the first time, they received a snapshot of the channel pushed by the server.
INTERVALWhen users in the channel reach the limit, the event notifications are sent at intervals rather than in real time.
REMOTE_JOINA remote user joined the channel.
REMOTE_LEAVEA remote user left the channel.
REMOTE_TIMEOUTA remote user's connection timed out.
REMOTE_STATE_CHANGEDA remote user's temporary state changed.
ERROR_OUT_OF_SERVICEThe user did not enable presence when joining the channel.

Service Types

ValueDescription
MESSAGEThe foundational services comprise the message channel, user channel, presence, storage, and lock services.
STREAMThe stream channel service.

Information

To fully utilize all services offered by Signaling, you can employ bitwise operations to simultaneously configure two service types.

Storage Event Types

ValueDescription
SNAPSHOTWhen a user subscribes to channel metadata or user etadata for the first time, or joins a channel, the local user receives notifications of this type of event.
SETOccurs when calling setChannelMetadata or setUserMetadata. This event only occurs in incremental data update mode.
UPDATEOccurs when calling methods to set, update, or delete the channel metadata or user metadata.
REMOVEOccurs when calling removeChannelMetadata or removeUserMetadata. This event only occurs in incremental data update mode.

Storage Types

ValueDescription
USERUser metadata event.
CHANNELChannel metadata event.

Topic Event Types

ValueDescription
SNAPSHOTThe snapshot of the topic when the user joined the channel.
REMOTE_JOINA remote user joined the channel.
REMOTE_LEAVEA remote user left the channel.

Troubleshooting

Refer to the following information for troubleshooting API calls.

ErrorInfo

type ErrorInfo = {
    error: boolean; // Whether there is an error in this operation
    operation: string; // The API name of this operation
    errorCode: number; // Error code.
    reason: string; // Error reason.
}

To find out the cause of the error and get the corresponding solution, use the errorCode field with the error codes table.

Error codes table

Refer to the following error codes table to identify and troubleshoot the problem:

Error codeError descriptionCause and solution
0RTM_ERROR_OKCorrect call
-10002RTM_ERROR_NOT_LOGINThe user called the API without logging in to Signaling, disconnected due to timeout, or actively logged out. Please log in to Signaling first.
-10003RTM_ERROR_INVALID_APP_IDInvalid App ID:
- Check that the App ID is correct.
- Ensure that Signaling has been activated for the App ID.
-10005RTM_ERROR_INVALID_TOKENInvalid Token:
- The token is invalid, check whether the Token Provider generates a valid Signaling Token.
-10006RTM_ERROR_INVALID_USER_IDInvalid User ID:
- Check if user ID is empty.
- Check if the user ID contains illegal characters.
-10008RTM_ERROR_INVALID_CHANNEL_NAMEInvalid channel name:
- Check if the channel name is empty.
- Check if the channel name contains illegal characters.
-10009RTM_ERROR_TOKEN_EXPIREDToken expired. Call renewToken to reacquire the Token.
-10010RTM_ERROR_LOGIN_NO_SERVER_RESOURCESServer resources are limited. It is recommended to log in again.
-10011RTM_ERROR_LOGIN_TIMEOUTLogin timeout. Check whether the current network is stable and switch to a stable network environment.
-10012RTM_ERROR_LOGIN_REJECTEDSDK login rejected by the server:
- Check tha Signaling is activated on your App ID.
- Check if the token or userId is banned.
-10013RTM_ERROR_LOGIN_ABORTEDSDK login interrupted due to unknown problem:
- Check that the current network is stable and switch to a stable network environment.
- The current userId is logged in.
-10015RTM_ERROR_LOGIN_NOT_AUTHORIZEDNo Signaling service permissions. Possible reasons include:
- Signaling is not enabled in Console.
- Service payment overdue.
- Account suspension.
-10017RTM_ERROR_DUPLICATE_OPERATIONRepeat the operation.
-10018RTM_ERROR_INSTANCE_ALREADY_RELEASEDRepeat rtm instantiation or RTMStreamChannel instantiation.
-10019RTM_ERROR_INVALID_CHANNEL_TYPEInvalid channel type. The SDK only supports the following channel types. Please use the correct value:
- MESSAGE: Message Channel
- STREAM: Stream Channel
- USER: User Channel
-10020RTM_ERROR_INVALID_ENCRYPTION_PARAMETERMessage encryption parameters are invalid.
- Check that the encryption key generated is a String.
- Check that the generated encryption salt is Uint8Array type and that the length is 32 bytes.
- Check that the encryption method matches the encryption key and the encryption salt.
-10021RTM_ERROR_OPERATION_RATE_EXCEED_LIMITATIONChannel metadata or User Metadata -related API call frequency is exceeding the limit. Please control the call frequency within 10/second.
-10022RTM_ERROR_SERVICE_NOT_SUPPORTEDThe service type is not supported. Check whether the service type you set in ServiceType is correct. This error code is only applicable to the private deployment function.
-10023RTM_ERROR_LOGIN_CANCELEDThe login operation has been canceled. Possible reasons are as follows:
- After calling the login method, if you call the method again before receiving the call result, the previous call operation will be canceled and the SDK will execute the next call.
- Calling the logout method to log out before successfully logging in.
-10025RTM_ERROR_NOT_CONNECTEDNot connected to the Signaling server. When you call APIs related to message subscription, metadata, or the Lock module, this error is reported if the Signaling service is not connected.
-10026RTM_ERROR_RENEW_TOKEN_TIMEOUTToken renewal timed out.
-11001RTM_ERROR_CHANNEL_NOT_JOINEDThe user has not joined the channel:
- The user is not online, offline or has not joined the channel
- Check for typos in userId.
-11002RTM_ERROR_CHANNEL_NOT_SUBSCRIBEDThe user has not subscribed to the channel:
- The user is not online, offline or has not joined the channel
- Check for typos in userId.
-11003RTM_ERROR_CHANNEL_EXCEED_TOPIC_USER_LIMITATIONThe number of subscribers to this topic exceeds the limit.
-11005RTM_ERROR_CHANNEL_INSTANCE_EXCEED_LIMITATIONThe number of created or subscribed channels exceeds the limit. See API usage limits for details.
-11006RTM_ERROR_CHANNEL_IN_ERROR_STATEChannel is not available. Please recreate the Stream Channel or resubscribe to the Message Channel.
-11007RTM_ERROR_CHANNEL_JOIN_FAILEDFailed to join this channel:
- Check if the number of joined channels exceeds the limit.
- Check if the channel name is illegal.
- Check if the network is disconnected.
-11008RTM_ERROR_CHANNEL_INVALID_TOPIC_NAMEInvalid topic name:
- Check whether the topic name contains illegal characters.
- Check if the topic name is empty.
-11009RTM_ERROR_CHANNEL_INVALID_MESSAGEInvalid message. Check whether the message type is legal, Signaling only supports string, Uint8Array type messages.
-11010RTM_ERROR_CHANNEL_MESSAGE_LENGTH_EXCEED_LIMITATIONMessage length exceeded limit. Check if the message payload size exceeds the limit:
- Message Channel single message package limit is 32 KB.
- Stream Channel single message package limit is 1 KB.
-11012RTM_ERROR_CHANNEL_NOT_AVAILABLEInvalid user list:
- Check if the user list is empty
- Check if the user list contains illegal items.
-11013RTM_ERROR_CHANNEL_TOPIC_NOT_SUBSCRIBEDThe topic is not subscribed.
-11014RTM_ERROR_CHANNEL_EXCEED_TOPIC_LIMITATIONThe number of topics exceeds the limit.
-11015RTM_ERROR_CHANNEL_JOIN_TOPIC_FAILEDFailed to join this topic. Check whether the number of added topics exceeds the limit.
-11016RTM_ERROR_CHANNEL_TOPIC_NOT_JOINEDThe topic has not been joined. To send a message, you need to join the Topic first.
-11017RTM_ERROR_CHANNEL_TOPIC_NOT_EXISTThe topic does not exist. Check that the topic name is correct.
-11018RTM_ERROR_CHANNEL_INVALID_TOPIC_METAThe meta parameters in the topic are invalid. Check if the meta parameter exceeds 256 bytes.
-11019RTM_ERROR_CHANNEL_SUBSCRIBE_TIMEOUTChannel subscription timed out. Check for broken connections.
-11020RTM_ERROR_CHANNEL_SUBSCRIBE_TOO_FREQUENTThe channel subscription operation is too frequent. Make sure that the subscription operation of the same channel within a 5 seconds interval does not exceed 2 attempts.
-11021RTM_ERROR_CHANNEL_SUBSCRIBE_FAILEDChannel subscription failed. Check if the number of subscribed channels exceeds the limit.
-11023RTM_ERROR_CHANNEL_ENCRYPT_MESSAGE_FAILEDMessage encryption failed:
- Check that the cipherKey is valid.
- Check that the salt is valid.
- Check if encryptionMode mode matches the cipherKey and salt.
-11024RTM_ERROR_CHANNEL_PUBLISH_MESSAGE_FAILEDMessage publishing failed. Check for broken connections.
-11026RTM_ERROR_CHANNEL_PUBLISH_MESSAGE_TIMEOUTMessage publishing timed out. Check for broken connections.
-11028RTM_ERROR_CHANNEL_LEAVE_FAILEDFailed to leave the channel. Check for broken connections.
-11029RTM_ERROR_CHANNEL_CUSTOM_TYPE_LENGTH_OVERFLOWCustom type length overflow. The length of the customType field must to be within 32 characters.
-11030RTM_ERROR_CHANNEL_INVALID_CUSTOM_TYPEcustomType field is invalid. Check the customType field for illegal characters.
-11033RTM_ERROR_CHANNEL_RECEIVER_OFFLINEWhen sending a user message, the remote user is offline:
- Check if the user ID set when calling the method is correct.
- Check if the remote user is logged in and online.
-12001RTM_ERROR_STORAGE_OPERATION_FAILEDStorage operation failed.
-12002RTM_ERROR_STORAGE_METADATA_ITEM_EXCEED_LIMITATIONThe number of Storage Metadata Items exceeds the limit.
-12003RTM_ERROR_STORAGE_INVALID_METADATA_ITEMInvalid Metadata Item.
-12004RTM_ERROR_STORAGE_INVALID_ARGUMENTInvalid argument.
-12005RTM_ERROR_STORAGE_INVALID_REVISIONInvalid Revision parameter.
-12006RTM_ERROR_STORAGE_METADATA_LENGTH_OVERFLOWMetadata overflows.
-12007RTM_ERROR_STORAGE_INVALID_LOCK_NAMEInvalid Lock name.
-12008RTM_ERROR_STORAGE_LOCK_NOT_ACQUIREDThe Lock was not acquired.
-12009RTM_ERROR_STORAGE_INVALID_KEYInvalid Metadata key.
-12010RTM_ERROR_STORAGE_INVALID_VALUEInvalid metadata value.
-12011RTM_ERROR_STORAGE_KEY_LENGTH_OVERFLOWMetadata key length overflow.
-12012RTM_ERROR_STORAGE_VALUE_LENGTH_OVERFLOWMetadata value length overflow.
-12014RTM_ERROR_STORAGE_OUTDATED_REVISIONOutdated Revision parameter.
-12015RTM_ERROR_STORAGE_NOT_SUBSCRIBEThis channel is not subscribed.
-12017RTM_ERROR_STORAGE_SUBSCRIBE_USER_EXCEED_LIMITATIONThe number of subscribers exceeds the limit.
-12018RTM_ERROR_STORAGE_OPERATION_TIMEOUTStorage operation timed out.
-12019RTM_ERROR_STORAGE_NOT_AVAILABLEThe Storage service is not available.
-13001RTM_ERROR_PRESENCE_NOT_CONNECTEDThe user is not connected to the system.
-13003RTM_ERROR_PRESENCE_INVALID_ARGUMENTInvalid argument.
-13004RTM_ERROR_PRESENCE_CACHED_TOO_MANY_STATESThe temporary user state cached before joining the channel exceeds the limit. See API usage limits for details.
-13005RTM_ERROR_PRESENCE_STATE_COUNT_OVERFLOWThe number of temporary user state key/value pairs exceeds the limit. See API usage limits for details.
-13006RTM_ERROR_PRESENCE_INVALID_STATE_KEYInvalid state key.
-13008RTM_ERROR_PRESENCE_STATE_KEY_SIZE_OVERFLOWPresence key length overflow.
-13009RTM_ERROR_PRESENCE_STATE_VALUE_SIZE_OVERFLOWPresence value overflow
-13011RTM_ERROR_PRESENCE_USER_NOT_EXISTThe user does not exist.
-13012RTM_ERROR_PRESENCE_OPERATION_TIMEOUTPresence operation timed out.
-13013RTM_ERROR_PRESENCE_OPERATION_FAILEDPresence operation failed.
-14001RTM_ERROR_LOCK_OPERATION_FAILEDLock operation failed.
-14002RTM_ERROR_LOCK_OPERATION_TIMEOUTLock operation timed out.
-14003RTM_ERROR_LOCK_OPERATION_PERFORMINGLock operation in progress.
-14004RTM_ERROR_LOCK_ALREADY_EXISTLock already exists.
-14005RTM_ERROR_LOCK_INVALID_NAMEInvalid Lock name.
-14006RTM_ERROR_LOCK_NOT_ACQUIREDThe Lock was not acquired.
-14007RTM_ERROR_LOCK_ACQUIRE_FAILEDFailed to acquire the Lock.
-14008RTM_ERROR_LOCK_NOT_EXISTThe Lock does not exist.
-14009RTM_ERROR_LOCK_NOT_AVAILABLELock service is not available.