Throttling & Slow mode

Last Edit: Sep 25 2020

Automatic Feature Throttling

When a channel has more than 100 active watchers Stream Chat automatically toggles off some features. This is to avoid performance degradation for end-users. Processing large amount of events can potentially increase CPU and memory usage on mobile and web apps.

  1. Read events and typing indicator events are discarded

  2. Watcher start/stop events are only sent once every 5 seconds

Channel Slow Mode

Slow mode is a channel feature to reduce message noise; when enabled users can’t post more than 1 message per cooldown interval.

The cooldown interval is configurable and can be anything between 1 and 120 seconds. For instance, if you enable slow mode and set the cooldown interval to 30 seconds a user will be able to post at most 1 message every 30 seconds. 

Moderators, admins and server-side API calls are not restricted by the cooldown period and can post messages as usual.

Slow mode is disabled by default and can be enabled/disabled by admins and moderators.


// enable slow mode and set cooldown to 1s
await channel.enableSlowMode(1);

// increase cooldown to 30s
await channel.enableSlowMode(30);

// disable slow mode
await channel.disableSlowMode();
                    

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

// enable slow mode and set cooldown to 1s
channel.enableSlowMode(cooldown: 1) { print($0) }

// increase cooldown to 30s
channel.enableSlowMode(cooldown: 30) { print($0) }

// disable slow mode
channel.disableSlowMode() { print($0) }
                    

When a user posts a message during the cooldown period, the API returns an error message. You can avoid hitting the APIs and instead show such limitation on the send message UI directly. When slow mode is enabled, channels include a cooldown field containing the current cooldown period in seconds.


const p = channel.sendMessage(msg);

if (channel.cooldown != null && channel.cooldown > 0) {
    p.then(() => {
        // first lock the UI so that the user is aware of the cooldown 
        disableSendMessageUI();
        // restore the UI after the cooldown is finished
        setTimeout(enableSendMessageUI, channel.cooldown * 1000);
    });
}

await p;
                    

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

channel.query { result in
  // Channel response includes cooldownDuration field
  let cooldownDuration = result.value!.channel.cooldownDuration
  // Send a message
  channel.send(message: .init(text: "Hello")) { (_) in
      // block the UI you're using
      // StreamChat doesn't have such a function, you need to implement it yourself
      disableMessageSendingUI()
      // Set a timer for cooldownDuration interval
      Timer.scheduledTimer(withTimeInterval: cooldownDuration, repeats: false) { (_) in
          enableMessageSendingUI()
      }
  }
}