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:

// Get a single channel
var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, "my-channel-id");

// Or multiple with optional filters
var channels = await Client.QueryChannelsAsync(new List<IFieldFilterRule>()
{
  ChannelFilter.Members.In(Client.LocalUserData.User)
});

// Subscribe to events
channel.MessageReceived += OnMessageReceived;
channel.MessageUpdated += OnMessageUpdated;
channel.MessageDeleted += OnMessageDeleted;

channel.ReactionAdded += OnReactionAdded;
channel.ReactionUpdated += OnReactionUpdated;
channel.ReactionRemoved += OnReactionRemoved;

channel.MemberAdded += OnMemberAdded;
channel.MemberRemoved += OnMemberRemoved;
channel.MemberUpdated += OnMemberUpdated;

channel.MembersChanged += OnMembersChanged;
channel.VisibilityChanged += OnVisibilityChanged;
channel.MuteChanged += OnMuteChanged;
channel.Truncated += OnTruncated;
channel.Updated += OnUpdated;
channel.WatcherAdded += OnWatcherAdded;
channel.WatcherRemoved += OnWatcherRemoved;
channel.UserStartedTyping += OnUserStartedTyping;
channel.UserStoppedTyping += OnUserStoppedTyping;
channel.TypingUsersChanged += OnTypingUsersChanged;

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

// Not supported in the Unity SDK

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:

public void SubscribeToClientEvents()
{
  Client.AddedToChannelAsMember += OnAddedToChannelAsMember;
  Client.RemovedFromChannelAsMember += OnRemovedFromChannel;
}

private void OnAddedToChannelAsMember(IStreamChannel channel, IStreamChannelMember member)
{
  // channel - new channel to which local user was just added
  // member - object containing channel membership information
}

private void OnRemovedFromChannel(IStreamChannel channel, IStreamChannelMember member)
{
  // channel - channel from which local user was removed
  // member - object containing channel membership information
}

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.

public void SubscribeToConnectionEvents()
{
  Client.Connected += OnConnected;
  Client.Disconnected += OnDisconnected;
  Client.ConnectionStateChanged += OnConnectionStateChanged;
}

private void OnConnected(IStreamLocalUserData localUserData)
{
}

private void OnDisconnected()
{
}

private void OnConnectionStateChanged(ConnectionState previous, ConnectionState current)
{
}

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)

Client.Connected -= OnConnected;
Client.Disconnected -= OnDisconnected;
Client.ConnectionStateChanged -= OnConnectionStateChanged;
© Getstream.io, Inc. All Rights Reserved.