Skip to main content

Get Conversations

Retrieves a list of conversations for a given chatroom or context. The response includes conversation details, metadata, and optional widgets for rendering additional UI components.

Steps to fetch conversations

  1. Create a GetConversationsRequest object with the required parameters.
  2. Call the getConversations() function using an instance of LMChatClient, passing the request object.
  3. Optionally implement the ConversationClientObserver protocol to observe real-time changes to conversations.
  4. Process the response LMResponse<GetConversationsResponse> to handle the fetched conversations or any errors.
// Create a request to fetch conversations
let request = GetConversationsRequest.builder()
.chatroomId("ENTER_CHATROOM_ID")
.type(.below)
.limit(20)
.observer(myConversationObserver)
.build()

// Fetch conversations using LMChatClient
if let response = LMChatClient.shared.getConversations(withRequest: request) {
if let conversations = response.data?.conversations {
// Handle success
print("Conversations retrieved: \(conversations.count)")
} else if let error = response.error {
// Handle error
print("Error fetching conversations: \(error.localizedDescription)")
}
} else {
print("Failed to initialize request.")
}

Models

GetConversationsRequest

Represents the request object used to fetch conversations.

VARIABLETYPEDESCRIPTIONOPTIONAL
chatroomIdStringID of the chatroom to fetch conversationsNo
typeGetConversationTypeType of conversations to retrieveNo
conversationConversation?Filter conversations starting from this itemYes
limitIntNumber of conversations to fetchNo
observerConversationClientObserverObserver for real-time updatesYes

Example Initialization

let request = GetConversationsRequest.builder()
.chatroomId("ENTER_CHATROOM_ID")
.type(.top)
.limit(50)
.observer(myObserverInstance)
.build()

GetConversationsResponse

Represents the response containing the list of conversations and associated metadata.

VARIABLETYPEDESCRIPTIONOPTIONAL
conversations[Conversation]List of fetched conversations
countInt?Total number of conversations✔️
widgets[String: Widget]?Optional UI components for additional context✔️

GetConversationType

Defines the type of conversations to fetch based on their relative position or context.

CASERAW VALUEDESCRIPTION
none0No specific conversation filter applied.
below1Fetch conversations below a specific ID.
above2Fetch conversations above a specific ID.
top3Fetch the top-most conversations.
bottom4Fetch the bottom-most conversations.

ConversationClientObserver

A protocol for observing changes in conversations. This protocol extends RealmObjectChangeObserver and provides methods to handle the initial state of conversations and track changes such as additions, updates, and removals.

Methods

METHODPARAMETERSDESCRIPTION
initialconversations: [Conversation]Called when the initial list of conversations is loaded.
onChangeremoved: [Int], inserted: [(Int, Conversation)], updated: [(Int, Conversation)]Called when changes occur, including removal, insertion, or updates of conversations.

Usage Example for Observer

class MyConversationObserver: ConversationClientObserver {

func initial(_ conversations: [Conversation]) {
print("Initial conversations loaded: \(conversations)")
}

func onChange(removed: [Int], inserted: [(Int, Conversation)], updated: [(Int, Conversation)]) {
print("Removed conversations at indices: \(removed)")
print("Inserted conversations: \(inserted)")
print("Updated conversations: \(updated)")
}
}

// Pass this observer to the request
let myObserver = MyConversationObserver()
let request = GetConversationsRequest.builder()
.chatroomId("ENTER_CHATROOM_ID")
.type(.below)
.limit(10)
.observer(myObserver)
.build()