Skip to main content

  1. App Server requests generated content from a large language model: App Server sends a streaming generation request with the prompt and context. The model continuously returns content segments, each containing content and a completion indicator.
  1. App Server sends segments: Based on the model output, App Server calls the IM Server SendStream API to send each segment to IM Server. Each call returns the segment delivery result.
  1. IM pushes the initial message: when App Server calls SendStream with seq = 1 and is_finished = false, IM Server pushes a jg:streamtext message to clients. The client receives this streaming message through the message listener.
  1. The client receives and appends segments: IM Server continuously pushes new content segments to clients. Each segment triggers an append event, which appends the content to the message and returns the messageId of jg:streamtext.
  1. Completion notification: After generation finishes, App Server calls SendStream with seq set to the last segment number and is_finished = true. IM Server pushes a completion notification to clients, triggering the complete event.

No Special Application-Layer Handling Is Required for These Cases

1. Reconnection after a network outage: The SDK automatically delivers complete notifications for new segments missed during the outage to the application layer.

2. Process termination or browser closure after generation starts: When users return online, they can still receive complete generated content or segments still being generated.

3. Automatic synchronization of generated content across devices: For example, content generated by user A on iOS remains visible after signing in on Web; in-progress content supports synchronized updates.

4. Generated-content timeout: After App Server begins sending a streaming message, it is completed automatically if is_finished = true is not set within 10 minutes. Content may be incomplete.

5. Retrieving message history: Generated messages can be retrieved directly through the message history APIs on each client. Their content is already assembled completely, and the application can render them by message type.

JIM.getInstance().getMessageManager().addListener("main", new IMessageManager.IMessageListener() {
/// message-receive callback
@Override
public void onMessageReceive(Message message) {
MessageContent content = message.getContent();
if (content instanceof StreamTextMessage) {
Log.d("TAG", "stream message did receive, content is " + ((StreamTextMessage) content).getContent());
}
}
}

JIM.getInstance().getMessageManager().addStreamMessageListener("main", new IMessageManager.IStreamMessageListener() {
@Override
public void onStreamTextMessageAppend(String messageId, String content) {
// messageId: ID of the corresponding streaming message
// content: content appended by this chunk; append content to the end of StreamTextMessage.content in the UI
}

@Override
public void onStreamTextMessageComplete(Message message) {
// message: complete streaming-message object
}
});