Windows

Updated at:

This document explains how to integrate the Real-Time Messaging (RTM) feature on Windows.

Introduction

Real-time messaging provides low latency, high concurrency, and high reliability. It is suitable for scenarios such as live chat and voice chat rooms.

The DingRTC real-time messaging feature is built on the meeting functionality. Therefore, you must join a meeting before you can use this feature.

Related concepts

Session: A real-time messaging session is uniquely identified by a sessionId. A session supports join, leave, and close operations. You must create and join a session before you can send or receive real-time messages. DingRTC lets you create multiple sessions with different sessionIds. These sessions do not interfere with each other.

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 UserID.

Integration method

  1. Before you join a meeting, set the RTM message listener.

    First, define the RTM callback listener class:

    class MyEngineEventListerner : public ding::rtc::RtcEngineEventListener,
      public ding::rtc::RtmEventListener
    {
    public:
      ... implement the interface defined by ding::rtc::RtcEngineEventListener
      // implement ding::rtc::RtmEventListener
      void OnRtmServerStateChanged(
        ding::rtc::RtmServerState state, int reason) override
      {
        You must implement this interface. The RTM feature can be used only after the state becomes ding::rtc::RtmServerState::Available.
    
        RtmInfo *rtm_info = &GetGlobalContext()->rtm_info_;
    
        if (state == ding::rtc::RtmServerState::Available) {
          The RTM feature is available.
        }
        else {
          The RTM feature is temporarily unavailable.
        }
      }
      void OnJoinSessionResult(
        const ding::rtc::String &sessionId, int result) override
      {
        The callback for JoinSession. It indicates whether joining the session was successful.
      }
      void OnLeaveSessionResult(
        const ding::rtc::String &sessionId, int result) override
      {
      }
      void OnCloseSessionResult(
        const ding::rtc::String &sessionId, int result) override
      {
      }
      void OnRemovedFromSession(
        const ding::rtc::String &sessionId, int reason) override
      {
      }
      void OnSessionCreate(
        const ding::rtc::String &sessionId) override
      {
      }
      void OnSessionClose(
        const ding::rtc::String &sessionId) override
      {
      }
      void OnSessionRemoteUserJoin(
        const ding::rtc::String &sessionId,
        const ding::rtc::String &uid) override
      {
      }
      void OnSessionRemoteUserLeave(
        const ding::rtc::String &sessionId,
        const ding::rtc::String &uid) override
      {
      }
      void OnMessage(const ding::rtc::String &sessionId,
        const ding::rtc::String &fromUid, bool broadcast,
        const ding::rtc::RtmData &data) override
      {
        // Messages from remote users are delivered here.
      }
    };
    

    Then, configure the engine:

    MyEngineEventListerner *mylistener = ...;
    ding::rtc::RtmClient *rtmclient = rtcengine->GetRtmClient();
    rtmclient->SetListener((ding::rtc::RtmEventListener *) mylistener);
    Note: A type cast is used here because MyEngineEventListerner uses C++ multiple inheritance.

  2. After you join the meeting, you can use the RTM feature when you receive the `OnRtmServerStateChanged` callback and the state is `ding::rtc::RtmServerState::Available`. First, create or join an RTM session:

    int rc = rtmclient->JoinSession(sessId);
    If rc == 0, you can expect to receive the OnJoinSessionResult callback to confirm if the join was successful.
    If rc < 0, the OnJoinSessionResult callback is not triggered. Check if the parameters are correct.
  3. Other members in the meeting are notified that a new session was created through the `MyEngineEventListerner::OnSessionCreate` event. They can join this session using the same code shown previously.

  4. Members in the same session can send real-time messages to each other.

    To send a broadcast message:

    ding::rtc::RtmData *d = new ding::rtc::RtmData;
    d->data = malloc(len);
    d->size = len;
    memcpy((uint8_t*)d->data, text, len);
    
    rc = rtmclient->BroadcastData(sessId, *d);
    Handle the return value. If it is 0, the message was sent successfully. For other error codes, see the API documentation.
    if (rc != 0)
    {
      std::string sErr;
      switch (rc)
      {
      case ding::rtc::RtmErrorCode::RtmInnerError:
            sErr = "Internal error"; break;
      case ding::rtc::RtmErrorCode::RtmServiceNotReady:
            sErr = "Rtm service not ready"; break;
      case ding::rtc::RtmErrorCode::RtmMsgLengthExceed:
            sErr = "Length exceeds limitation"; break;
      case ding::rtc::RtmErrorCode::RtmMsgQueueFull:
            sErr = "Qps exceeds limitation"; break;
      case ding::rtc::RtmErrorCode::BadSessionState:
            sErr = "Session lost or not joined"; break;
      case ding::rtc::RtmErrorCode::RtmReceiverNotExist:
            sErr = "Remote user left session"; break;
      default:
            sErr = "Unknown error"; break;
      }
      std::wstring wErr = utf82utf16(sErr);
      MessageBox(wErr.c_str(), _T("Failed to send message"), MB_ICONHAND);
    }
    
    Remember to delete the data after use:
    free ((void *)d->data);
    delete d;
    

    To send a peer-to-peer message:

    The code is similar to broadcasting, but you specify the recipient's userid:
    rc = rtmclient->SendData(sessionId, remoteuid, *d);

    Remote users in the session receive messages through the `MyEngineEventListerner::OnMessage` callback:

      void OnMessage(const ding::rtc::String &sessionId,
        const ding::rtc::String &fromUid, bool broadcast,
        const ding::rtc::RtmData &data) override
      {
        // Messages from remote users are delivered here.
      }
  5. For more complex applications that maintain a session member list in the user interface, you can listen for the following events:

    void OnSessionRemoteUserJoin(const ding::rtc::String &sessionId,
      const ding::rtc::String &uid) override;
    void OnSessionRemoteUserLeave(const ding::rtc::String &sessionId,
      const ding::rtc::String &uid) override;
    And channel-level callbacks:
    virtual void OnRemoteUserOffLineNotify(const char *uid,
      ding::rtc::RtcEngineUserOfflineReason reason) override;

  6. For simple applications, all members can join a default session when they join the meeting by calling `JoinSession("default");` after the `RtmServerState::Available` state is reached. Then, they can send broadcast or peer-to-peer messages.

  7. Exception handling

    In addition to handling the return values of sent messages, you should also listen for and handle the following events:

    Removed from a session for a specific reason:
    void OnRemovedFromSession(const ding::rtc::String &sessionId, int reason) override;
    A session was closed:
    void OnSessionClose(const ding::rtc::String &sessionId) override;

Notes

  1. When a user leaves a meeting, the SDK internally clears all session information. The real-time messaging feature becomes unavailable.

  2. If a remote user leaves the channel without calling `leaveSession`, you might not receive the `onSessionRemoteUserLeave` event for that user. In this scenario, your application must remove the user from all session member lists after receiving the `onRemoteUserOffLineNotify` event for that user.