# Event listeners (/en/realtime-media/rtm/build/send-and-receive-messages/add-event-listener/flutter)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

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](../../index) page.

## Implement event listeners

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

### Add an event listener

Signaling uses the `addListener` method to process messages and event notifications. Each message and event notification has a corresponding named parameter, where you pass a callback that implements your own processing logic. Refer to the following code to register event handlers:

```dart
rtmClient.addListener(
    // Handle message event
    message: (MessageEvent event) {
        final channelType = event.channelType;      // The channel type, `stream` `message`, or `user`
        final channelName = event.channelName;      // The name of the channel that this message came from
        final topicName = event.channelTopic;       // (Stream channel only) The name of the topic that this message came from
        final messageType = event.messageType;      // The message type, `string` or `binary`
        final customType = event.customType;        // User defined type
        final publisher = event.publisher;          // The message publisher
        final message = event.message;              // The message payload
        final timestamp = event.timestamp;          // Event timestamp
    },
    // Handle presence event
    presence: (PresenceEvent event) {
        final action = event.type;                  // The event type, should be one of `snapshot`, `interval`, `remoteJoinChannel`, `remoteLeaveChannel`, `remoteTimeout`, `remoteStateChanged`, `errorOutOfService`.
        final channelType = event.channelType;      // The channel type, should be `stream`, `message` or `user`
        final channelName = event.channelName;      // The name of the channel this event came from
        final publisher = event.publisher;          // The user who triggered this event
        final states = event.stateItems;            // User state payload, only for the stateChanged event
        final interval = event.interval;            // Interval payload, only for the interval event
        final snapshot = event.snapshot;            // Snapshot payload, only for the snapshot event
        final timestamp = event.timestamp;          // Event timestamp
    },
   // Handle topic event
    topic: (TopicEvent event) {
        final action = event.type;                  // The event type, should be one of `snapshot`, `remoteJoinTopic`, `remoteLeaveTopic`
        final channelName = event.channelName;      // The name of the channel this event came from
        final publisher = event.publisher;          // The user who triggered this event
        final topicInfos = event.topicInfos;        // Topic information payload
        final timestamp = event.timestamp;          // Event timestamp
    },
    // Handle storage event
    storage: (StorageEvent event) {
        final channelType = event.channelType;      // The channel type, should be `stream`, `message` or `user`
        final target = event.target;                // Which channel or user triggered this event
        final storageType = event.storageType;      // The storage type, should be `user`, `channel`
        final action = event.eventType;             // The event type, should be one of `snapshot`, `set`, `remove`, `update` or `none`
        final data = event.data;                    // USER_METADATA or CHANNEL_METADATA payload
        final timestamp = event.timestamp;          // Event timestamp
    },
    // Handle lock event
    lock: (LockEvent event) {
        final channelType = event.channelType;      // The channel type, should be `stream`, `message` or `user`
        final channelName = event.channelName;      // The name of the channel this event came from
        final action = event.eventType;             // The action type, should be one of `lockSet`, `lockRemoved`, `lockAcquired`, `lockReleased`, `lockExpired`, `snapshot`
        final lockDetailList = event.lockDetailList;    // Lock event payload
        final timestamp = event.timestamp;          // Event timestamp
    },
    // Handle link state event
    linkState: (LinkStateEvent event) {
        final currentState = event.currentState;    // The current link state
        final previousState = event.previousState;  // The previous link state
        final serviceType = event.serviceType;      // The service type, Should be "stream" or "message".
        final operation = event.operation;          // Which operation triggered this state changed
        final reason = event.reason;                // The reason for this state change
        final affectedChannels = event.affectedChannels;  // Which channels are affected
        final unrestoredChannels = event.unrestoredChannels;    // Which channels are unrestored
        final isResumed = event.isResumed;
        final timestamp = event.timestamp;          // Event timestamp
    },
    // Handle token event
    token : (TokenEvent event) {
        final channelName = event.channelName;      // The channel for which the token will expire
    });
```

> **Info**
>
> The `message` field in `MessageEvent` is of type `Uint8List`. When you receive a string message, use the `utf8.decode()` method from the `dart:convert` library to convert it to a `String`.
>
> ```dart
> // Convert message from Uint8List to a String
> String message = utf8.decode(event.message!);
> ```

### Remove event listeners

To avoid performance degradation caused by memory leaks, errors, and null pointer exceptions, best practice is to unregister an event handler when you no longer need to use it. To be able to unregister an event listener, avoid using anonymous functions when adding event listeners. The following code shows how to add and remove event listeners:

```dart
// Define a message event handling function
void onMessageReceived(MessageEvent event) {
  print('Received message event: $event');
}

// Register the message event listener
rtmClient.addListener(
  message: onMessageReceived
);

// Remove the message event listener
rtmClient.removeListener(
  message: onMessageReceived
);
```

### Signaling events

Signaling SDK offers the following events:

| Event Listener | Description                                                                                                                                                                                                                                                                                                                     |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `message`      | Receive 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 information such as channel name, channel type, topic name, event sender, and message payload data type.         |
| `presence`     | Receive online status event notifications of 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.                                          |
| `topic`        | Receive 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.                                                                                                                            |
| `storage`      | Receive 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. |
| `lock`         | Receive 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.                                                                                            |
| `linkState`    | Receive 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.                                                                         |
| `token`        | Receive event notifications when the client token is about to expire.                                                                                                                                                                                                                                                           |

For details on the parameters passed in by each event, refer to [Event listeners](/en/api-reference/api-ref/signaling/flutter#event-listeners) in the API reference.
