Skip to main content
Android
iOS
macOS
Web
Linux C++
Unity

Add an event listener

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 RtmEventListener 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 RtmEventListener:


_49
// Create an RtmEventListener instance
_49
RtmEventListener eventListener = new RtmEventListener() {
_49
@Override
_49
public void onMessageEvent(MessageEvent event) {
_49
// Triggered when messages are received from remote users
_49
}
_49
_49
@Override
_49
public void onPresenceEvent(PresenceEvent event) {
_49
// Triggered when the presence info changes
_49
}
_49
_49
@Override
_49
public void onTopicEvent(TopicEvent event) {
_49
}
_49
_49
@Override
_49
public void onLockEvent(LockEvent event) {
_49
// Triggered when the channel lock info changes
_49
}
_49
_49
@Override
_49
public void onStorageEvent(StorageEvent event) {
_49
// Triggered when the subscribed storage info changes
_49
}
_49
_49
@Override
_49
public void onConnectionStateChange(String channelName, RtmConstants.RtmConnectionState state,
_49
RtmConstants.RtmConnectionChangeReason reason) {
_49
// Triggered when the connection state changes
_49
}
_49
_49
@Override
_49
public void onLinkStateEvent(LinkStateEvent event) {
_49
// Triggered when the link state changes
_49
}
_49
_49
@Override
_49
public void onTokenPrivilegeWillExpire(String channelName) {
_49
}
_49
// Triggered when the token is about to expire
_49
};
_49
_49
RtmConfig rtmConfig = new RtmConfig();
_49
// Specify the event listener in the client configuration
_49
rtmConfig.eventListener = eventListener;
_49
_49
// Create an RtmClient instance
_49
mRtmClient = RtmClient.create(rtmConfig);

Remove event listeners

To avoid performance degradation caused by memory leaks, errors, and exceptions, best practice is to unregister an event handler when you no longer need to use it.


_1
mRtmClient.removeEventListener(eventListener);

The RtmClient automatically destroys event listeners when you call release.

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. The event payload data includes channel name, channel type, topic name, event sender, message payload data type, and other information.
onPresenceEventReceive online status event notifications from remote users in all message channels you have subscribed to and stream channels you have joined. The event payload data includes channel name, channel type, event type, event sender, user temporary status data, and other information.
onTopicEventReceive notifications of topic change events in all the stream channels you have joined. The event payload data includes information such as channel name, event type, topic name, and event sender.
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. The event payload data includes information such as channel name, channel type, event type, and specific metadata.
onLockEventReceive all lock event notifications in the message channel you have subscribed to and stream channel you have joined. The event payload data contains information such as channel name, channel type, event type, and lock details.
onLinkStateEventReceive event notifications for client network connection status changes, including the connection status before and after the change, service type, operation type that caused the change, reason for the change, channel list, and other information.
onTokenPrivilegeWillExpireReceive event notifications that the client token is about to expire.
onConnectionStateChanged(Deprecated) Receive event notifications of client network connection status changes, including channel name, connection status, and reason for change.

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

Signaling