Listening for Events

As soon as you call watch on a Channel or queryChannels you’ll start to listen to these events. You can hook into specific events:

channel.on("message.deleted").listen((Event event) {
 print("message ${event.message.id} was deleted");
});

You can also listen to all events at once: (Full list of events is on events object page)

channel.on().listen((Event event) {
 print("received a new event of type ${event.type}");
});

Client Events

Not all events are specific to channels. Events such as the user’s status has changed, the users’ unread count has changed, and other notifications are sent as client events. These events can be listened to through the client directly:

// subscribe to all client events and log the unread_count field
client.on().listen((event) => {
	if (event.totalUnreadCount != null) {
		print('unread messages count is now: ${event.totalUnreadCount}');
	}
 
	if (event.unreadChannels !== null) {
		print('unread channels count is now: ${event.unreadChannels}');
	}
});

// the initial count of unread messages is returned by client.setUser
final user = await client.setUser(user, userToken);
console.log(`you have ${user.me.totalUnreadCount} unread messages on ${user.me.unreadChannels} channels.`);

Connection Events

The official SDKs make sure that a connection to Stream is kept alive at all times and that chat state is recovered when the user’s internet connection comes back online. Your application can subscribe to changes to the connection using client events.

client.on('connection.changed', (e) => {
  if (e.online) {
    print('the connection is up!');
  } else {
    print('the connection is down!');
  }
});

Stop Listening for Events

It is a good practice to unregister event handlers once they are not in use anymore. Doing so will save you from performance degradations coming from memory leaks or even from errors and exceptions (i.e. null pointer exceptions)

final subscription = channel.on().listen((Event event) {
 print("received a new event of type ${event.type}");
});

subscription.cancel();
© Getstream.io, Inc. All Rights Reserved.