Invites

Inviting Users

Stream Chat provides the ability to invite users to a channel. Upon invitation, the end-user will receive a notification that they were invited to the specified channel.

Most of our SDKs have a helper method for member invitation which uses the Update Channel API endpoint.

See the following for an example on how to invite a user by user ID:

// Android SDK
ChannelClient channelClient = client.channel("messaging", "general");

List<String> memberIds = Arrays.asList("thierry", "tommaso");
Map<String, Object> data = new HashMap<>();
data.put("invites", Arrays.asList("nick"));

channelClient.create(memberIds, data).enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
 });
 

// Backend SDK
Channel.update("messaging", "general")
  .invites(Arrays.asList("thierry", "tommaso"))
  .request();

Accepting an Invite

In order to accept an invite, you must use call the acceptInvite method. The acceptInvite method accepts an object with an optional message property. Please see below for an example of how to call acceptInvite :

The message can be used for system messages for display within the channel (e.g. “Nick joined this channel!”).

Unread counts are not incremented for a channel for which a user is a member of but has a pending invite.

// Android SDK
channelClient.acceptInvite("Nick joined this channel!").enqueue(result -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Channel.update("messaging", "general")
  .acceptInvite(true)
  .userId("nick")
  .message(MessageRequestObject.builder().text("Nick joined the channel").build())
  .request();

Rejecting an Invite

To reject an invite, call the rejectInvite method. This method does not require a user ID as it pulls the user ID from the current session in store from the connectUser call.

// Android SDK
channelClient.rejectInvite().enqueue(result -> {
  if (result.isSuccess()) {
    // Invite rejected
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Channel.update("messaging", "general")
  .acceptInvite(false)
  .userId("nick")
  .request();

Query for Accepted Invites

Querying for accepted invites is done via the queryChannels method. This allows you to return a list of accepted invites with a single call. See below for an example:

// Android SDK
FilterObject filter = Filters.eq("invite", "accepted");
int offset = 0;
int limit = 10;
QuerySorter<Channel> sort = new QuerySortByField<>();
int messageLimit = 0;
int memberLimit = 0;
QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit, memberLimit);

client.queryChannels(request).enqueue(result -> {
  if (result.isSuccess()) {
    List<Channel> channels = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Channel.queryMembers().type("messaging").id("general").filterCondition("invite", "accepted").request();

Query for Rejected Invites

Similar to querying for accepted invites, you can query for rejected invites with queryChannels. See below for an example:

// Andoid SDK
FilterObject filter = Filters.eq("invite", "rejected");
int offset = 0;
int limit = 10;
QuerySorter<Channel> sort = new QuerySortByField<>();
int messageLimit = 0;
int memberLimit = 0;
QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit, memberLimit);

client.queryChannels(request).enqueue(result -> {
  if (result.isSuccess()) {
    List<Channel> channels = result.data();
  } else {
    // Handle result.error()
  }
});

// Backend SDK
Channel.queryMembers()
  .type("messaging")
  .id("general")
  .filterCondition("invite", "rejected")
  .request();

Query for Pending Invites

Similar to querying for accepted and rejected invites, you can query for pending invites with queryChannels. See below for an example:

FilterObject filter = Filters.eq("invite", "pending");
int offset = 0;
int limit = 10;
QuerySorter<Channel> sort = new QuerySortByField<>();
int messageLimit = 0;
int memberLimit = 0;
QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit, memberLimit);

client.queryChannels(request).enqueue(result -> {
  if (result.isSuccess()) {
    List<Channel> channels = result.data();
  } else {
    // Handle result.error()
  }
});
© Getstream.io, Inc. All Rights Reserved.