Invites

LAST EDIT Feb 26 2021

Inviting Users

Copied!

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:

1
2
3
4
5
6
7
const invite = client.channel('messaging', 'awesome-chat', { 
    name: 'Founder Chat', 
    members: ['thierry', 'tommaso', 'nick'], 
    invites: ['nick'], 
}); 
 
await invite.create();
1
2
3
4
5
6
7
$channel = $client->Channel('messaging', 'team-chat-2',  
	[ 
			'members'=>['thierry', 'jenny', 'elon'], 
			'invites'=>['elon'] 
	] 
); 
$state   = $channel->create('thierry');
1
2
3
4
5
6
7
8
final invite = client.channel("messaging", id: "awesome-chat", 
    extraData: { 
      "name": "Founder Chat", 
      "members": ["thierry", "tommaso", "nick"], 
      "invites": ["nick"], 
    }); 
 
await invite.create();
1
2
3
4
5
6
7
8
9
10
11
12
13
val channelClient = client.channel("messaging", "general") 
val data = mapOf( 
    "members" to listOf("thierry", "tommaso"), 
    "invites" to listOf("nick") 
) 
 
channelClient.create(data).enqueue { result -> 
    if (result.isSuccess) { 
        val channel = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
ChannelClient channelClient = client.channel("messaging", "general"); 
             
Map<String, Object> data = new HashMap<>(); 
data.put("members", Arrays.asList("thierry", "tommaso")); 
data.put("invites", Arrays.asList("nick")); 
 
channelClient.create(data).enqueue(result -> { 
    if (result.isSuccess()) { 
        Channel channel = result.data(); 
    } else { 
        // Handle result.error() 
    } 
});
1
// coming soon

Accepting an Invite

Copied!

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!").
1
2
3
4
5
6
7
8
9
10
// 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'});
1
2
3
4
5
// initialize the channel 
$channel = $client->Channel('messaging', 'team-chat-5'); 
 
// accept the invite 
$accept = $channel->acceptInvite(['user_id'=>'elon']);
1
2
final channel = client.channel("messaging", id: "awesome-chat"); 
await channel.acceptInvite();
1
2
3
4
5
6
7
8
9
channelClient.acceptInvite( 
    message = "Nick joined this channel!" 
).enqueue { result -> 
    if (result.isSuccess) { 
        val channel = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
channelClient.acceptInvite("Nick joined this channel!").enqueue(result -> { 
    if (result.isSuccess()) { 
        Channel channel = result.data(); 
    } else { 
        // Handle result.error() 
    } 
});
1
// coming soon

Rejecting an Invite

Copied!

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.

1
2
3
4
await channel.rejectInvite(); 
 
//server side  
await channel.rejectInvite({'user_id':'rob'});
1
$reject = $channel->rejectInvite(['user_id'=>'elon']);
1
await channel.rejectInvite();
1
2
3
4
5
6
7
channelClient.rejectInvite().enqueue { result ->  
    if (result.isSuccess) {  
        // Invite rejected  
    } else {  
        // Handle result.error()  
    }  
}
1
2
3
4
5
6
7
channelClient.rejectInvite().enqueue(result -> { 
    if (result.isSuccess()) { 
        // Invite rejected 
    } else { 
        // Handle result.error() 
    } 
});
1
// coming soon

Query for Accepted Invites

Copied!

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:

1
2
3
4
5
6
7
8
const invites = await client.queryChannels({ 
    invite: 'accepted', 
}); 
 
//server side (query invites for user rob) 
const invites = await client.queryChannels({ 
    invite: 'accepted', 
},{},{'user_id':'rob'});
1
$invites = $client->queryChannels(['invite' => 'accepted'],[], ['user_id' => 'jenny']);
1
final invites = await client.queryChannels(filter: {"invite": "accepted"});
1
2
3
4
5
6
7
8
9
10
11
12
val request = QueryChannelsRequest( 
    filter = Filters.eq("invite", "accepted"), 
    offset = 0, 
    limit = 10 
) 
client.queryChannels(request).enqueue { result -> 
    if (result.isSuccess) { 
        val channels: List<Channel> = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FilterObject filter = Filters.eq("invite", "accepted"); 
int offset = 0; 
int limit = 10; 
QuerySort<Channel> sort = new QuerySort<>(); 
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() 
    } 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import StreamChat 
 
let controller = chatClient.channelListController( 
    query: .init( 
        filter: .equal("invite", to: "accepted") 
    ) 
) 
 
controller.synchronize { error in 
    if let error = error { 
        // handle error 
        print(error) 
    } else { 
        // access channels 
        print(controller.channels) 
         
        // load more if needed 
        controller.loadNextChannels(limit: 10) { _ in 
            // handle error / access channels 
        } 
    } 
}

Query for Rejected Invites

Copied!

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

1
2
3
4
5
6
7
8
const rejected = client.queryChannels({ 
    invite: 'rejected', 
}); 
 
//server side (query invites for user rob) 
const invites = await client.queryChannels({ 
    invite: 'rejected', 
},{},{'user_id':'rob'});
1
$invites = $client->queryChannels(['invite' => 'rejected'],[], ['user_id' => 'jenny']);
1
final rejected = await client.queryChannels(filter: {"invite": "rejected"});
1
2
3
4
5
6
7
8
9
10
11
12
val request = QueryChannelsRequest( 
    filter = Filters.eq("invite", "rejected"), 
    offset = 0, 
    limit = 10 
) 
client.queryChannels(request).enqueue { result -> 
    if (result.isSuccess) { 
        val channels: List<Channel> = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FilterObject filter = Filters.eq("invite", "rejected"); 
int offset = 0; 
int limit = 10; 
QuerySort<Channel> sort = new QuerySort<>(); 
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() 
    } 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import StreamChat 
 
let controller = chatClient.channelListController( 
    query: .init( 
        filter: .equal("invite", to: "rejected") 
    ) 
) 
 
controller.synchronize { error in 
    if let error = error { 
        // handle error 
        print(error) 
    } else { 
        // access channels 
        print(controller.channels) 
         
        // load more if needed 
        controller.loadNextChannels(limit: 10) { _ in 
            // handle error / access channels 
        } 
    } 
}