Livestream Chat

Introduction

The Stream Chat SDK provides a LivestreamChannelController optimized for livestreams when performance is critical. Unlike the regular ChatChannelController, it doesn’t store messages in the local database, eliminating database read/write overhead. It is also more memory efficient since it supports setting a maximum number of messages.

Comparison

The LivestreamChannelController is more limited in terms of features in comparison to the ChatChannelController. Here is a list of features that each controller supports to help you decide which one to use.

FeatureRegular ChatLive Chat
PerformanceGreatExcellent
Memory UsageHigherLower
Messages Capping
Reactions
Attachments Rendering
Attachment Upload Progress
Commands
Read Indicators
Typing Indicators
Threads
Offline Support

Usage

Basic Usage

The LivestreamChannelController has the same interface as the regular ChatChannelController, making it easy to switch between them. You can create a livestream controller using the same methods you’re already familiar with:

// Create a livestream channel controller
let query = ChannelQuery(cid: .init(type: .livestream, id: "my-livestream"))
let livestreamController = chatClient.livestreamChannelController(for: query)

The controller works exactly like a regular channel controller - you can send messages, add reactions, and handle all the same events:

// Send a message
livestreamController.createNewMessage(text: "Hello livestream!")

// Add a reaction
livestreamController.addReaction(to: messageId, type: "love")

Message Limiting

One of the key features of the LivestreamChannelController is the ability to limit the number of messages kept in memory. This helps prevent memory issues during long livestreams with high message volume.

You can configure the maximum number of messages using maxMessageLimitOptions as well as the amount of messages that are discarded when the limit is reached:

// Set a limit of 100 messages and discard 20 messages when the limit is reached.
livestreamController.maxMessageLimitOptions = MaxMessageLimitOptions(
    maxLimit: 100,
    discardAmount: 20
)

// You can also make the discard amount to 1.
livestreamController.maxMessageLimitOptions = MaxMessageLimitOptions(
    maxLimit: 100,
    discardAmount: 1
)

When the message limit is reached, older messages are automatically discarded to make room for new ones.

Pause and Resume for Pagination

If you need to support pagination (loading older messages) while maintaining message limits, you can use the pause and resume functionality. This is useful, because when the user scrolls up to load older pages, the discarding of messages needs to be paused.

// Pause new messages updates in the controller.
// The new messages will be discarded until resume is called.
// Call this when the users starts scrolling the message list.
livestreamController.pause()

// Load older messages.
livestreamController.loadPreviousMessages()

// Resume new message updates. Call this once the user reaches
// the bottom of the message list or creates a new message.
livestreamController.resume()

This is a feature that most livestream apps support like Twitch, here is an example on how the UI can look like when the user scrolls up while in the middle of a livestream.

Livestream Chat Paused

Demo Implementation

At the moment the SDK does not provide a default UI component that uses this controller. But you can find an example implementation in the UIKit Demo App, which demonstrates:

  • Setting up the livestream controller
  • Configuring message limits
  • Handling pause/resume functionality

To test the livestream controller in the demo app:

  1. Open the Stream Chat Demo App
  2. Login with any user and open the channel list
  3. Swipe a channel in the channel list
  4. Tap on ”…”
  5. Select “Show as Livestream Controller”

This will open the livestream chat interface where you can see the performance benefits and test the message limiting functionality.

© Getstream.io, Inc. All Rights Reserved.