Invites

Last Edit: Nov 27 2020

Inviting Users

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

See the following for an example on how to invite a user by adding an invites array containing the user ID:


const invite = client.channel('messaging', 'awesome-chat', {
    name: 'Founder Chat',
    members: ['thierry', 'tommaso', 'nick'],
    invites: ['nick'],
});

await invite.create();
                    

$channel = $client->Channel('messaging', 'team-chat-2', 
	[
			'members'=>['thierry', 'jenny', 'elon'],
			'invites'=>['elon']
	]
);
$state   = $channel->create('thierry');
                    

final invite = client.channel("messaging", id: "awesome-chat",
    extraData: {
      "name": "Founder Chat",
      "members": ["thierry", "tommaso", "nick"],
      "invites": ["nick"],
    });

await invite.create();
                    

val members = listOf("thierry", "tommaso")
val invites = listOf("nick")
val data = mutableMapOf<String, Any>()

data["members"] = members
data["invites"] = invites

channelController.create(channelType, channelId, data).enqueue {
    val channel = it.data()
}
                    

List<String> members = new ArrayList<>();
members.add("thierry");
members.add("tomasso");

List<String> invites = new ArrayList<>();
invites.add("nick");

Map<String, Object> data = new HashMap<>();
data.put("members", members);
data.put("invites", invites);

client.createChannel(channelType, channelId, data).enqueue(result -> {
    Channel newChannel = result.data();
    return Unit.INSTANCE;
});
                    

let channel = Client.shared.channel(type: .messaging, id: "general")
let john = User(id: "john")

channel.invite(member: john.asMember) { (result) in
    // handle result
}
                    

Accepting an Invite

In order to accept an invite, you must use call the acceptInvite method. The acceptInvite method accepts and 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!").

// initialize the channel
const channel = client.channel('messaging', 'awesome-chat');

// accept the invite
await channel.acceptInvite({
    message: { text: 'Nick joined this channel!' },
});

// accept the invite server side 
await channel.acceptInvite({'user_id':'nick'});
                    

// initialize the channel
$channel = $client->Channel('messaging', 'team-chat-5');

// accept the invite
$accept = $channel->acceptInvite(['user_id'=>'elon']);
                    

final channel = client.channel("messaging", id: "awesome-chat");
await channel.acceptInvite();
                    

channelController.acceptInvite("Nick joined this channel!").enqueue {
    val channel = it.data()
}
                    

channelController.acceptInvite("Nick joined this channel!").enqueue(result -> Unit.INSTANCE);
                    

let channel = Client.shared.channel(type: .messaging, id: "general")

channel.acceptInvite { (result) in
    // handle result
}
// or
let message = Message(text: "John has joined!")
channel.acceptInvite(with: message) { (result) in /**/ }
                    

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 setUser call.


await channel.rejectInvite();

//server side 
await channel.rejectInvite({'user_id':'rob'});
                    

$reject = $channel->rejectInvite(['user_id'=>'elon']);
                    

await channel.rejectInvite();
                    

channelController.rejectInvite().enqueue {
    val channel = it.data()
}
                    

channelController.rejectInvite().enqueue(result -> Unit.INSTANCE);
                    

let channel = Client.shared.channel(type: .messaging, id: "general")

channel.rejectInvite { (result) in
    // handle result
}
// or
let message = Message(text: "John has rejected the invite!")
channel.rejectInvite(with: message) { (result) in /**/ }
                    

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:


const invites = await client.queryChannels({
    invite: 'accepted',
});

//server side (query invites for user rob)
const invites = await client.queryChannels({
    invite: 'accepted',
},{},{'user_id':'rob'});
                    

$invites = $client->queryChannels(['invite' => 'accepted'],[], ['user_id' => 'jenny']);
                    

final invites = await client.queryChannels(filter: {"invite": "accepted"});
                    

val offset = 0
val limit = 10
val request = QueryChannelsRequest(FilterObject("invite", "accepted"), offset, limit)
client.queryChannels(channelType, channelId, request).enqueue {
    val channels = it.data()
}
                    

FilterObject filter = new FilterObject("invite", "accepted");
int offset = 0;
int limit = 10;
int messageLimit = 10;
QuerySort sort = new QuerySort();

QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit);

client.queryChannels(request).enqueue(result -> {
    List<Channel> channels = result.data();
    return Unit.INSTANCE;
});
                    

let query = ChannelsQuery(filter: .equal("invite", to: "accepted"))
Client.shared.queryChannels(query: query) { (result) in
    // handle result
}
                    

Query for Rejected Invites

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


const rejected = client.queryChannels({
    invite: 'rejected',
});

//server side (query invites for user rob)
const invites = await client.queryChannels({
    invite: 'rejected',
},{},{'user_id':'rob'});
                    

$invites = $client->queryChannels(['invite' => 'rejected'],[], ['user_id' => 'jenny']);
                    

final rejected = await client.queryChannels(filter: {"invite": "rejected"});
                    

val offset = 0
val limit = 10
val request = QueryChannelsRequest(FilterObject("invite", "rejected"), offset, limit)
client.queryChannels(request).enqueue {
    val channels = it.data()
}
                    

FilterObject filter = new FilterObject("invite", "rejected");
int offset = 0;
int limit = 10;
int messageLimit = 10;
QuerySort sort = new QuerySort();

QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit);

client.queryChannels(request).enqueue(result -> {
    List<Channel> channels = result.data();
    return Unit.INSTANCE;
});
                    

let query = ChannelsQuery(filter: .equal("invite", to: "rejected"))
Client.shared.queryChannels(query: query) { (result) in
    // handle result
}