Agora Real-time Messaging (RTM) SDK for JavaScript

2.2.4 · active · verified Sun Apr 19

The Agora Real-time Messaging (RTM) SDK for JavaScript, currently stable at version 2.2.4, provides robust real-time interaction capabilities for client applications. This SDK enables developers to integrate feature-rich, scalable, and proven real-time engagement solutions, moving beyond basic messaging to support complex scenarios like conference control, interactive games, metaverse applications, online education, e-commerce, and smart devices. Version 2.x represents a significant iteration, offering enhanced functions, improved performance, and a better user experience compared to its predecessors. While an explicit release cadence isn't stated, the continuous iteration to version 2.x and ongoing updates (such as v2.2.8 released Feb 2026) suggest active development and maintenance. Key differentiators include its proven reliability with over 3000 customers, wide applicability across diverse use cases, and strong focus on scalable real-time messaging, often complementing Agora's RTC (Real-time Communication) offerings for voice and video.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Agora RTM client, logs in with a user ID and optional token, joins a specified channel, and sends a text message to that channel. It also sets up event listeners for connection state changes and incoming peer/channel messages.

import AgoraRTM from 'agora-rtm-sdk';

const APP_ID = process.env.AGORA_APP_ID ?? '';
const UID = process.env.AGORA_UID ?? String(Math.floor(Math.random() * 100000));
const CHANNEL_NAME = 'my_agora_channel';

async function startRTMClient() {
  if (!APP_ID) {
    console.error('AGORA_APP_ID is not set. Please set it as an environment variable or replace the placeholder.');
    return;
  }

  const client = AgoraRTM.createInstance(APP_ID);
  console.log('RTM Client created.');

  client.on('ConnectionStateChange', (newState, reason) => {
    console.log(`Connection state changed to ${newState}, reason: ${reason}`);
    if (newState === 'CONNECTED') {
      console.log('Successfully connected to Agora RTM.');
    }
  });

  client.on('MessageFromPeer', ({ text }, peerId) => {
    console.log(`Peer message from ${peerId}: ${text}`);
  });

  try {
    await client.login({ uid: UID, token: undefined }); // Use undefined for temporary token in debug mode
    console.log(`Logged in as ${UID}.`);

    const channel = client.createChannel(CHANNEL_NAME);
    console.log(`Channel '${CHANNEL_NAME}' created.`);

    channel.on('ChannelMessage', ({ text }, senderId) => {
      console.log(`Channel message from ${senderId}: ${text}`);
    });

    await channel.join();
    console.log(`Joined channel '${CHANNEL_NAME}'.`);

    await channel.sendMessage({ text: 'Hello, Agora RTM Channel!' });
    console.log('Message sent to channel.');

  } catch (err) {
    console.error('Agora RTM error:', err);
  }
}

startRTMClient();

view raw JSON →