JAVASCRIPT • IN-APP CHAT • STEP-BY-STEP

How to add secure in-app chat with a JavaScript SDK

Build secure, room-based chat inside your own web app with the RTCExpress JavaScript chat SDK. This guide covers user tokens, joining a room, sending a message, receiving messages, and loading recent history.

1. Create a chat project and two test users

Chat is included in the RTCExpress Free plan. Create a project in the customer portal, then use your own backend to give two test users short-lived tokens for the same room, for example support-room-42.

Your backend must keep the App Secret private. It authenticates your user, checks that they may enter the room, and requests the scoped user token. The browser receives only that token.

2. Initialize the JavaScript SDK and join a room

Initialize one SDK instance for the signed-in user. Register the room and message listeners before joining so the interface is ready when the server confirms membership.

import { RTCExpress } from "rtcexpress";

const rtc = new RTCExpress();

rtc.on("roomJoined", async ({ roomId }) => {
  const history = await rtc.getMessageHistory(roomId, { limit: 30 });
  renderMessages(history.messages);
});

rtc.on("message", (message) => appendMessage(message));

await rtc.init({ serverUrl, appId, userId, token });
rtc.joinRoom("support-room-42");

Install the SDK with npm install rtcexpress, or start from the JavaScript starter, which already includes it.

3. Send a message from your interface

When the person presses Send, validate the visible input in your interface and pass the message text to the SDK. The message event only fires for messages from other users; your own message is not echoed back. So add it to your list yourself when you send it. sendMessage returns a client message ID you can keep with it.

sendButton.addEventListener("click", () => {
  const text = messageInput.value.trim();
  if (!text) return;

  const clientMessageId = rtc.sendMessage(text);
  appendMessage({ clientMessageId, fromUserId: userId, text, sentAt: Date.now() });
  messageInput.value = "";
});

Do not send passwords, App Secrets, access tokens, or private keys in messages. Your application should define its own retention, moderation, and reporting rules for customer content.

4. Test the actual chat experience

  • Use two different user IDs in the same project and room.
  • Verify that each person sees new messages without refreshing.
  • Reconnect one browser and confirm that the interface handles connection changes clearly.
  • Load message history only after room membership is confirmed.
  • Try a user token for another room and ensure your backend denies access.

Where chat fits with voice and video

Use chat as the default conversation surface, then let your product move to a voice or video interaction when users need a faster, more personal discussion. See the voice, video, and chat API guide to choose the right feature, or explore voice calling and video calling.

Secure chat SDK questions

How is an in-app chat SDK secured?

Your backend must authenticate the person, confirm that they may enter the room, and issue a short-lived scoped token. Keep the App Secret only on the server.

Can I use this chat SDK in an enterprise product?

You can integrate chat into your own business application, but you must implement your own identity, moderation, retention, compliance, and access-control requirements. Review your security requirements before production.

Start building in-app chat

Create a project, issue room-scoped test tokens from your backend, and connect two users to the same room.

Create a free projectOpen API reference