Skip to main content

Streaming Messages

tip

This feature is supported starting from version 5.16.1.

Users can display streaming messages in the conversation list component of the chat UI either as complete messages (Figure 1) or message digests (Figure 2) using the imkit_stream plugin. The message type for SDK streaming messages is StreamMessage (type identifier: RC:StreamMsg).

Limitations

The streaming message feature currently has the following restrictions:

  • The message content length is limited to 10,000 characters.
  • Supports streaming message reception in one-to-one chat and group chat conversations.

Usage

  1. Add RC Chat's interface library (IMKit) as a remote dependency in your app's build.gradle.

    dependencies {
    ...
    // The imkit_stream plugin depends on the im_kit component
    api 'cn.rongcloud.sdk:im_kit:x.y.z'
    // Streaming message plugin
    api 'cn.rongcloud.sdk:imkit_stream:x.y.z'
    }
    tip

    The latest version numbers for each SDK may vary and could be x.y.z.h. Check the RC official SDK download page or RC's Maven repository for updates.

For other import methods, refer to Import SDK.

  1. Add the following code:
// For example, in the Application class (order of initialization doesn't matter)
RongConfigCenter.conversationConfig().addMessageProvider(new StreamMessageItemProvider());
  1. For sending streaming messages, see Send Streaming Messages.

Content Display

Streaming messages support Markdown, Text, and HTML types. For specific field values, refer to Streaming Messages. HTML type support was added in version 5.20.0.

For HTML type, pay attention to the following. SDKs for Markdown and Text types don't require special handling.

Content Security

The IMKit SDK uses the native WebView component to load html-type streaming messages but does not process the content. Developers must ensure content security.

Developers can embed CSP tags in HTML to enhance security.

tip

CSP is part of HTML and should be processed by the application server before being sent to the RC server. Use AI models to format output and system prompts to add specific content.

Sample Code

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; img-src 'self';">
<!-- Other content -->
</head>
<body>
<!-- Page content -->
</body>
</html>

Dark Mode

The IMKit SDK adapts WebView themes using the CSS rule prefers-color-scheme, but this has system version limitations on mobile devices and depends on the app's theme settings.

  • Xiaomi devices: MIUI has customized dark mode implementation. If the app doesn't handle it specially, switching the system theme will automatically change the IMKit SDK and WebView to dark mode.
  • Non-Xiaomi devices: Currently, Android Kit and WebView do not support this feature.
tip

prefers-color-scheme is part of HTML and must be processed by the developer's AppServer before being sent to the RC server. Use AI models to format output and system prompts to add specific content.

Sample Code

@media (prefers-color-scheme: dark) {
.day.dark-scheme {
background: #333;
color: white;
}
.night.dark-scheme {
background: black;
color: #ddd;
}
}

@media (prefers-color-scheme: light) {
.day.light-scheme {
background: white;
color: #555;
}
.night.light-scheme {
background: #eee;
color: black;
}
}