Muting Channels

LAST EDIT Apr 12 2021

Messages added to a channel will not trigger push notifications, nor change the unread count for the users that muted it.

By default, mutes stay in place indefinitely until the user removes it; however, you can optionally set an expiration time.

The list of muted channels and their expiration time is returned when the user connects.

Channel Mute

Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const reply = await client.setUser(user, token); 
 
// reply.me.channel_mutes contains the list of channel mutes 
console.log(reply.me.channel_mutes); 
 
const channel = client.channel("messaging", "channel-id"); 
 
// mute channel for current user 
await channel.mute(); 
 
// mute channel for a user (server-side) 
await channel.mute({ user_id: userId }); 
 
// mute a channel for 2 weeks 
await channel.mute({ expiration: moment.duration(2, 'weeks') }); 
 
// mute a channel for 10 seconds 
await channel.mute({ expiration: 10000 }); 
 
// check if channel is muted 
// { muted: true | false, createdAt: Date | null, expiresAt: Date | null} 
channel.muteStatus();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Mute a channel 
val channelClient = client.channel("messaging", "general") 
channelClient.mute().enqueue { result -> 
    if (result.isSuccess) { 
        // Channel is muted 
    } else { 
        // Handle result.error() 
    } 
} 
 
// Get list of muted channels when user is connected 
client.connectUser(User("user-id"), "token") 
    .enqueue { result -> 
        if (result.isSuccess) { 
            val user = result.data().user 
            // Result contains the list of channel mutes 
            val mutes: List<ChannelMute> = user.channelMutes 
        } 
    } 
 
// Get updates about muted channels 
client.subscribeFor<ChannelsMuteEvent> { event -> 
    val mutes: List<ChannelMute> = event.channelsMute 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Mute a channel 
ChannelClient channelClient = client.channel("messaging", "general"); 
channelClient.mute().enqueue(result -> { 
    if (result.isSuccess()) { 
        // Channel is muted 
    } else { 
        // Handle result.error() 
    } 
}); 
 
// Get list of muted channels when user is connected 
User user = new User(); 
user.setId("user-id"); 
client.connectUser(user, "token").enqueue(result -> { 
    if (result.isSuccess()) { 
        // Result contains the list of channel mutes 
        List<ChannelMute> mutes = result.data().getUser().getChannelMutes(); 
    } 
}); 
 
// Get updates about muted channels 
client.subscribeFor( 
        new Class[]{ChannelsMuteEvent.class}, 
        channelsMuteEvent -> { 
            List<ChannelMute> mutes = ((ChannelsMuteEvent) channelsMuteEvent).getChannelsMute(); 
        } 
);
1
2
3
4
5
6
7
8
9
10
11
import Stream Chat 
 
let controller = chatClient.channelController(for: .init(type: .messaging, id: "general")) 
 
// mute channel 
controller.muteChannel { error in 
    if let error = error { 
        // handle error 
        print(error) 
    } 
}
1
2
3
4
5
6
7
8
9
10
11
// mute channel for current user 
await channel.mute(); 
 
// mute a channel for 2 weeks 
await channel.mute(expiration: Duration(days: 15)); 
 
// mute a channel for 10 seconds 
await channel.mute(expiration: Duration(seconds: 10)); 
 
// check if channel is muted 
channel.isMuted;
Messages added to muted channels do not increase the unread messages count.

Query Muted Channels

Copied!

Muted channels can be filtered or excluded by using the muted in your query channels filter.

1
2
3
4
5
// retrieve all channels excluding muted ones 
await client.queryChannels({ members: { $in: [userId] }, muted: false }); 
 
// retrieve all muted channels 
await client.queryChannels({ muted: true });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Filter for all channels excluding muted ones 
val notMutedFilter = Filters.and( 
    Filters.eq("muted", false), 
    Filters.`in`("members", listOf(currentUserId)), 
) 
 
// Filter for muted channels 
val mutedFilter = Filters.eq("muted", true) 
 
// Executing a channels query with either of the filters 
client.queryChannels(QueryChannelsRequest( 
    filter = filter, // Set the correct filter here 
    offset = 0, 
    limit = 10, 
)).enqueue { result -> 
    if (result.isSuccess) { 
        val channels: List<Channel> = result.data() 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
Client.shared.queryChannels(filter: .equal("muted", to: true)) { (result) in 
    // handle result 
} 
 
Client.shared.queryChannels(filter: .equal("muted", to: false)) { (result) in 
    // handle result 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Filter for all channels excluding muted ones 
FilterObject notMutedFilter = Filters.and( 
        Filters.eq("muted", false), 
        Filters.in("members", Arrays.asList(currentUserId)) 
); 
 
// Filter for muted channels 
FilterObject mutedFilter = Filters.eq("muted", true); 
 
// Executing a channels query with either of the filters 
int offset = 0; 
int limit = 10; 
QuerySort<Channel> sort = new QuerySort<>(); 
int messageLimit = 0; 
int memberLimit = 0; 
client.queryChannels(new QueryChannelsRequest( 
        filter, // Set the correct filter here 
        offset, 
        limit, 
        sort, 
        messageLimit, 
        memberLimit)).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
// retrieve all channels excluding muted ones 
await client.queryChannels( 
    filter: { 
      'members': { 
        r'$in': [userId], 
      }, 
      'muted': false, 
    } 
  ); 
 
// retrieve all muted channels 
await client.queryChannels( 
    filter: { 
      'muted': true, 
    } 
  );

Remove a Channel Mute

Copied!

Whenever you need to unmute, you are able to.

1
2
3
4
5
// unmute channel for current user 
await channel.unmute(); 
 
// unmute channel for a user (server-side) 
await channel.unmute({ user_id: userId });
1
2
3
4
5
6
7
8
// Unmute channel for current user 
channelClient.unmute().enqueue { result -> 
    if (result.isSuccess) { 
        // Channel is unmuted 
    } else { 
        // Handle result.error() 
    } 
}
1
2
3
4
5
6
7
8
// Unmute channel for current user 
channelClient.unmute().enqueue(result -> { 
    if (result.isSuccess()) { 
        // Channel is unmuted 
    } else { 
        // Handle result.error() 
    } 
});
1
2
3
4
5
6
7
8
9
10
11
import Stream Chat 
 
let controller = chatClient.channelController(for: .init(type: .messaging, id: "general")) 
 
// unmute channel 
controller.unmuteChannel { error in 
    if let error = error { 
        // handle error 
        print(error) 
    } 
}
1
2
// unmute channel for current user 
await channel.unmute();