Watching a Channel

The call to channel.watch does a few different things in one API call:

  • It creates the channel if it doesn’t exist yet (if this user has the right permissions to create a channel)

  • It queries the channel state and returns members, watchers and messages

  • It watches the channel state and tells the server that you want to receive events when anything in this channel changes

To start watching a channel

The examples below show how to watch a channel. Note that you need to be connected as a user before you can watch a channel.

var filters = new IFieldFilterRule[]
{
  UserFilter.Id.EqualsTo("other-user-id")
};
// find user you want to start chat with
var users = await Client.QueryUsersAsync(filters);

var otherUser = users.First();
var localUser = Client.LocalUserData.User;

// Get channel by ID
var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, channelId: "my-channel-id");

// Get channel with users combination
var channelWithUsers
  = await Client.GetOrCreateChannelWithMembersAsync(ChannelType.Messaging,
    new[] { localUser, otherUser });

// Access properties
Debug.Log(channel.Name);
Debug.Log(channel.Members);
Debug.Log(channel.Name);
Debug.Log(channel.Name);

// Subscribe to events so you can react to updates
channel.MessageReceived += OnMessageReceived;
channel.MessageUpdated += OnMessageUpdated;
channel.MessageDeleted += OnMessageDeleted;
channel.ReactionAdded += OnReactionAdded;
channel.ReactionUpdated += OnReactionUpdated;
channel.ReactionRemoved += OnReactionRemoved;

// You can also access all currently watched channels via
foreach (var c in Client.WatchedChannels)
{
  // Every queried channel is automatically watched and starts receiving updates from the server
}

Response Schema

NameTypeDescription
configobjectThe configuration for the channel type.
channelobjectThe Channel object.
onlineintegerNumber of online members.
watchersobjectUsers that are watching this channel. Represented as a mapping from the user id to the user object.
membersobjectChannel members. Represented as a mapping from the user id to the user object.
readobjectRead messages grouped by user id. Represented as a mapping from the user id to the message object.

Watching a channel only works if you have connected as a user to the chat API

Watchers vs Members

The concepts of watchers vs members can require a bit of clarification:

  • members : a permanent association between a user and a channel. If the user is online and not watching the channel they will receive a notification event, if they are offline they will receive a push notification.

  • watchers : the list of watchers is temporary. It’s anyone who is currently watching the channel.

Being able to send messages, and otherwise engage with a channel as a non-member requires certain permissions. For example, we have pre-configured permissions on our livestream channel type to allow non-members to interact, but in the messaging channel type, only members of the channel can interact.

Watching Multiple Channels

The default queryChannels API returns channels and starts watching them. There is no need to also use channel.watch on the channels returned from queryChannels

var localUser = Client.LocalUserData.User;

var filters = new IFieldFilterRule[]
{
  // Get channels where local user is a member
  ChannelFilter.Members.In(localUser.Id)
};

var channels = await Client.QueryChannelsAsync(filters);

// Get all currently watched channels
foreach (var channel in channels)
{
  // Access properties
  Debug.Log(channel.Name);
  Debug.Log(channel.Members);
  Debug.Log(channel.Name);
  Debug.Log(channel.Name);

  // Subscribe to events so you can react to updates
  channel.MessageReceived += OnMessageReceived;
  channel.MessageUpdated += OnMessageUpdated;
  channel.MessageDeleted += OnMessageDeleted;
  channel.ReactionAdded += OnReactionAdded;
  channel.ReactionUpdated += OnReactionUpdated;
  channel.ReactionRemoved += OnReactionRemoved;
}

Stop Watching a Channel

To stop receiving channel events:

await channel.StopWatchingAsync();

Watcher Count

To get the watcher count of a channel:

Debug.Log(channel.WatcherCount);

Paginating Channel Watchers with channel.query

// Will be implemented soon, raise a GitHub issue if you need this https://github.com/GetStream/stream-chat-unity/issues/new

The maximum limit that can be used is 300, and the maximum offset that can be used is 10 000.

Listening to Changes in Watchers

A user already watching the channel can listen to users starting and stopping watching the channel with the realtime events:

public async Task ListenToChangesInWatchers()
{
  // Get IStreamChannel reference by Client.GetOrCreateChannel or Client.QueryChannels
  var channel = await Client.GetOrCreateChannelWithIdAsync(ChannelType.Messaging, channelId: "my-channel-id");

  // Subscribe to events
  channel.WatcherAdded += OnWatcherAdded;
  channel.WatcherRemoved += OnWatcherRemoved;
}

private void OnWatcherAdded(IStreamChannel channel, IStreamUser user)
{
}

private void OnWatcherRemoved(IStreamChannel channel, IStreamUser user)
{
}
© Getstream.io, Inc. All Rights Reserved.