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
- Create a GetConversationsRequest object with the required parameters.
- Call the
getConversations()function using an instance ofLMChatClient, passing therequestobject. - Optionally implement the ConversationClientObserver protocol to observe real-time changes to conversations.
- 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.
| VARIABLE | TYPE | DESCRIPTION | OPTIONAL |
|---|---|---|---|
chatroomId | String | ID of the chatroom to fetch conversations | No |
type | GetConversationType | Type of conversations to retrieve | No |
conversation | Conversation? | Filter conversations starting from this item | Yes |
limit | Int | Number of conversations to fetch | No |
observer | ConversationClientObserver | Observer for real-time updates | Yes |
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.
| VARIABLE | TYPE | DESCRIPTION | OPTIONAL |
|---|---|---|---|
conversations | [Conversation] | List of fetched conversations | |
count | Int? | 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.
| CASE | RAW VALUE | DESCRIPTION |
|---|---|---|
none | 0 | No specific conversation filter applied. |
below | 1 | Fetch conversations below a specific ID. |
above | 2 | Fetch conversations above a specific ID. |
top | 3 | Fetch the top-most conversations. |
bottom | 4 | Fetch 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
| METHOD | PARAMETERS | DESCRIPTION |
|---|---|---|
initial | conversations: [Conversation] | Called when the initial list of conversations is loaded. |
onChange | removed: [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()