Event listeners

Updated

Receive event notifications.

To receive message and event notifications when you subscribe to or join channels, you add an event listener. An event listener is triggered by the SDK to inform the client about Signaling events and state changes. The listener enables you to respond to events like successful connection to Signaling, token expiration, received messages, and other presence, storage, and lock events.

Prerequisites

Ensure that you have integrated the Signaling SDK in your project and implemented the framework functionality from the SDK quickstart page.

Implement event listeners

This section shows how to use the Signaling SDK to implement event listeners.

Add an event listener

Signaling uses an IRtmEventHandler instance to process messages and event notifications. Each message and event notification has a corresponding event handler, where you implement your own processing logic. Refer to the following code to create and use an instance of IRtmEventHandler:

class RtmHandler: public IRtmEventHandler {
    void onMessageEvent(const MessageEvent &event) {}
    void onTopicEvent(const TopicEvent &event) {}
    void onLinkStateEvent(const LinkStateEvent &event) {}
    void onLoginResult(RTM_ERROR_CODE errorCode) {
        if (errorCode != RTM_ERROR_OK) {
            printf("login rtm failed error is %d reason is %s\n", errorCode, getErrorReason(errorCode));
        } else {
            printf("login rtm success\n");
        }
    }
    void onStorageEvent(const StorageEvent &event) {}
    void onLockEvent(const LockEvent &event) {}
}

RtmConfig cfg;
cfg.appId = "your_appid";
cfg.userId = "your_name";
// Specify the event listener in the client configuration
cfg.eventHandler = new RtmHandler();

// Create an IRtmClient instance
IRtmClient* rtm_client = createAgoraRtmClient();
int ret = rtm_client->initialize(cfg);

Remove event listeners

To avoid performance degradation caused by memory leaks, errors, and null pointer exceptions, best practice is to unregister the event handler when you no longer need to use it. For example, if you don't want to use the void onLockEvent(const LockEvent &event) {} event, just delete the definition of the function.

Signaling events

Signaling SDK offers the following events:

Event ListenerDescription
onMessageEventReceive message notifications from all the message channels you have subscribed to, or topic message notifications subscribed to from all the stream channels you have joined.
onPresenceEventReceive online status event notifications from remote users in all the message channels you have subscribed to and stream channels you have joined.
onTopicEventReceive notifications of topic change events in all the stream channels you have joined.
onStorageEventReceive all channel metadata event notifications in all message channels you have subscribed to and stream channels you have joined, as well as user metadata event notifications from all subscribed users.
onLockEventReceive all lock event notifications in the message channels you have subscribed to and stream channels you have joined.
onLinkStateEventReceive event notifications for client network connection status changes.
onTokenPrivilegeWillExpireReceive event notifications that the client token is about to expire.

For details on the parameters passed in by each event, refer to Event listeners in the API reference.