Ohos
This topic explains how to integrate the Real-Time Messaging (RTM) feature on an Ohos client.
Introduction
Real-time messaging provides low latency, high concurrency, and high reliability, making it ideal for scenarios such as live chat and voice chat rooms.
The real-time messaging feature in DingRTC is designed for use within meetings. You must join a meeting before you can use this feature.
Related concepts
Session: A real-time messaging session identified by a unique session ID. A session supports join, leave, and close operations. Before you can send or receive real-time messages, you must create and join a session. DingRTC lets you create multiple independent sessions, each with a different session ID.
Broadcast message: A message sent to all other members in a session.
Peer-to-peer message: A message sent to a specific user in a session, identified by their user ID.
Integration method
Before joining a meeting, obtain the `DingRtmClient` instance from `DingRtc`.
let rtmClient = dingRtc.getRtmClient();Set a callback listener for the `DingRtmClient` instance.
rtmClient.setRtmListener(new RtmListEventListener()) class RtmListEventListener extends DingRtmEventListener { onRtmServerStateChanged(state: DingRtcConstants.RtmServerState, reason: number): void { console.error('onRtmServerStateChanged - state:', state, ', reason:', reason); } onJoinSessionResult(sessionId: string, result: number): void { } onLeaveSessionResult(sessionId: string, result: number): void { } onCloseSessionResult(sessionId: string, result: number): void { } onRemovedFromSession(sessionId: string, reason: number): void { } onSessionCreated(sessionId: string): void { } onSessionClosed(sessionId: string): void { } onSessionRemoteUserJoin(sessionId: string, uid: string): void { } onSessionRemoteUserLeave(sessionId: string, uid: string): void { } onMessage(sessionId: string, fromUid: string, broadcast: boolean, message: Uint8Array, size: number): void { } }After joining the conference, the session initiator creates and joins the session:
rtmClient.joinSession(sessionId); // If the session does not exist, calling joinSession first creates the session and then joins it.Other members in the meeting receive an `onSessionCreate` event notification. They can then join the same session.
rtmClient.joinSession(sessionId); // If the session exists, calling joinSession directly joins it.When you join a session with other members, or when another member joins a session that you are in, you receive an `onSessionRemoteUserJoin` event notification. To send peer-to-peer messages, your application layer must maintain a list of session members.
onSessionRemoteUserJoin(sessionId: string, uid: string): void { }Similarly, when a member leaves the session, you receive an `onSessionRemoteUserLeave` event notification. You must update the session member list accordingly.
onSessionRemoteUserLeave(sessionId: string, uid: string): void { }The SDK supports sending and receiving data as UTF-8 encoded strings and binary data. On Android, all messages are passed as a byte array parameter. Sending broadcast messages:
let msg = "test123"; // For converting between Uint8Array and String, see https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkts-91-V5 let data = StringUtil.stringToUint8Array(msg); rtmClient.broadcastData(sessionId, data);To send a peer-to-peer message:
let msg = "test123"; // For converting between Uint8Array and String, see https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkts-91-V5 let data = StringUtil.stringToUint8Array(msg); rtmClient.sendData(sessionId, uid, data);To receive a message:
onMessage(sessionId: string, fromUid: string, broadcast: boolean, message: Uint8Array, size: number): void { console.info('RtmListEventListener onMessage [NAPI] message: ' + StringUtil.uint8ArrayToString(message)); }If you no longer need the real-time messaging feature, you can leave the session.
rtmClient.leaveSession(sessionId);After you leave a session, you will no longer receive broadcast messages from other members, and they will be unable to send peer-to-peer messages to you.
If a session is no longer needed, you can close it.
rtmClient.closeSession(sessionId);After a session is closed, all its members, including you, receive an `onSessionClose` event notification. The session becomes inactive.
Notes
Obtain the `DingRtmClient` instance and set its listener before you call `joinChannel`. Otherwise, you might miss some RTM messages.
When you leave a meeting, the SDK automatically clears all session information. The real-time messaging feature becomes inactive.
If a remote user calls `leaveChannel` without first calling `leaveSession`, you might not receive the `onSessionRemoteUserLeave` event notification for that user. When you receive the `onRemoteUserOffLineNotify` event, your application must manually remove that user from all session member lists.