Moderate misbehaving users as a chat user
Regular chat users (without secretKey) can mute undesirable users to hide their messages on the client side.
Mute list behavior:
- Soft limit: 200 users
- Default: session-only persistence
- Optional: persist across sessions (32 KiB server limit applies)
Mute list limit
Persisted lists exceeding 32 KiB (~200 users) trigger HTTP 413 errors. Users remain muted for the session but won't persist.
Affected methods:
channel.onMessageReceived()channel.join()channel.getHistory()chat.listenForEvents()chat.getEventsHistory()
Requires App Context
Enable App Context in the Admin Portal to mute users.
Mute users as a regular chat user
Mute a specific user on all channels.
Method signature
- Async/await
- Closure
1func muteUser(userId: String) async throws
1func muteUser(
2 userId: String,
3 completion: ((Result<Void, Error>) -> Void)?
4)
Input
completion#- Completion closure called with the result of the operation.
Output
This method calls the completion closure with a Result<Void, Error> that succeeds when the data has been synced with the server. If syncMutedUsers is not enabled, the completion always succeeds.
Errors
If the size of the mute list exceeds 32 KiB (roughly 200 users), you'll get the HTTP 413 (Request Entity Too Large) error.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
- Async/await
- Closure
1let userToMute = "user_rks"
2Task {
3 do {
4 try await chat.mutedUsersManager.muteUser(userId: userToMute)
5 print("User muted successfully")
6 } catch {
7 print("Failed to mute user: \(error)")
8 }
9}
1// Assumes a "ChatImpl" reference named "chat"
2let userToMute = "user_rks"
3chat.mutedUsersManager.muteUser(userId: userToMute) { result in
4 switch result {
5 case .success:
6 print("User muted successfully")
7 case .failure(let error):
8 print("Failed to mute user: \(error)")
9 }
10}
Unmute users as a regular chat user
Remove a user from the mute list to see their messages and events again.
Method signature
- Async/await
- Closure
1func unmuteUser(userId: String) async throws
1func unmuteUser(
2 userId: String,
3 completion: ((Result<Void, Error>) -> Void)?
4)
Input
completion#- Completion closure called with the result of the operation.
Output
This method calls the completion closure with a Result<Void, Error> that succeeds when the data has been synced with the server. If syncMutedUsers is not enabled, the completion always succeeds.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
- Async/await
- Closure
1let userToUnmute = "user_rks"
2Task {
3 do {
4 try await chat.mutedUsersManager.unmuteUser(userId: userToUnmute)
5 print("User unmuted successfully")
6 } catch {
7 print("Failed to unmute user: \(error)")
8 }
9}
1// Assumes a "ChatImpl" reference named "chat"
2let userToUnmute = "user_rks"
3chat.mutedUsersManager.unmuteUser(userId: userToUnmute) { result in
4 switch result {
5 case .success:
6 print("User unmuted successfully")
7 case .failure(let error):
8 print("Failed to unmute user: \(error)")
9 }
10}
Check muted users
Inspect the mute list to see which users are muted.
Method signature
1var mutedUsers: Set<String> { get }
Output
This property returns a Set<String> where each String is a user ID of a muted user.
Sample code
1// Assumes a "ChatImpl" reference named "chat"
2for userId in chat.mutedUsersManager.mutedUsers {
3 print("Muted user: \(userId)")
4}
Persist the mute list
Set syncMutedUsers: true during client initialization to persist the mute list across sessions.
Access Manager permissions
With Access Manager and syncMutedUsers enabled, grant these permissions (replace $currentUserId with the actual user ID):
readonPN_PRIV.$currentUserId.mute1channelupdate,delete,getonPN_PRIV.$currentUserId.mute1user
Listen to restriction changes
onRestrictionChanged() notifies the current user whenever an admin changes their mute or ban status on any channel. Use this to react in real time — for example, to show a notification or adjust UI when the user is muted or banned.
You can also use user.stream.restrictionChanges() for an AsyncStream-based approach.
Method signature
1user.onRestrictionChanged(
2 callback: @escaping (Restriction) -> Void
3) -> AutoCloseable
The Restriction struct carries the restriction payload:
1public struct Restriction {
2 public let userId: String
3 public let channelId: String
4 public let ban: Bool
5 public let mute: Bool
6 public let reason: String?
7}
Input
callbackrequired#- Closure called with a
Restrictionwhenever the user's moderation status changes on a channel.
Output
AutoCloseable#- An object you must retain. When released or closed, the listener stops.