📘

If you want to just query the user data and take care of the UI by yourself, this part describes how to do it. For a quick start with built-in UI components, you can use our Messaging UI Components.

Integrating the SDK

The NablaMessagingCore library is compatible with iOS 13 and higher.

Make sure to import NablaMessagingCore when you want to use it.

Watch user conversations list

You can watch the list of conversations a user has access to. The callback closure will be called every time there's a change in those conversations and will always return all the data (you don't need to accumulate it in your own properties):

private var conversationsWatcher: PaginatedWatcher?

self.conversationsWatcher = NablaMessagingClient.shared.watchConversations { result in
    switch result {
    case let .success(list):
        print("User has \(list.conversations.count) conversations")
    case let .failure(error):
        print("Error \(error)")
    }
}

watchConversations() returns a PaginatedWatcher which also happens to be a Cancellable.

You must retain Cancellable objects if you want their callback argument to keep getting called. If you release them, they will automatically cancel() themselves and callback will never be called again.

PaginatedWatcher also provides loadMore() and loadMore(numberOfItems:) methods that will fetch more items in the corresponding list. The completion closure of those methods does not return the items, instead, the callback closure used for watchConversations() will be called again with the added items. Use it as your only source of truth (you don't need to accumulate conversations in your own properties).

private var conversationsWatcher: PaginatedWatcher?
private var loadMoreAction: Cancellable?

self.loadMoreAction = conversationsWatcher?.loadMore { result in
    switch result {
    case .success:
        print("More conversations successfully loaded")
    case let .failure(error):
        print("Error \(error)")
    }
}

Create a new conversation

If you want your user to be able to start a new conversation, you can allow to create it into your app directly:

private var createConversationAction: Cancellable?

self.createConversationAction = NablaMessagingClient.shared.createConversation { result in
    switch result {
    case let .success(conversation):
        print("New conversation created: \(conversation)")
    case let .failure(error):
        print("Error \(error)")
    }
}

Similar to loadMore(), createConversation() will cause the callback of watchConversations() to be called again with the new conversation added to the list.

Watch a conversation

To watch the messages of a conversation, the same pagination principles applies:

private var itemsWatcher: PaginatedWatcher?

self.itemsWatcher = NablaMessagingClient.shared.watchItems(
  ofConversationId: conversationId
) { [weak self] result ->
  switch result {
    case let .success(conversationWithItems):
      print("Received \(watchItems.items.count) messages")
    case let .error(error):
      print("Error \(error)")
  }
}

You can also watch for a conversation details update (like a Provider typing status):

private var conversationWatcher: Cancellable?

self.conversationWatcher = NablaMessagingClient.shared.watchConversation() { [weak self] result ->
  switch result {
    case let .success(conversation):
      print("Conversation updated: \(conversation)")
    case let .error(error):
      print("Error \(error)")
  }
}

Send a new message

Creating and sending your message

The user can send a message in an existing conversation:

private var sendMessageAction: Cancellable?

let messageInput  = MessageInput.Text(content: "Hello world!")
self.sendMessageAction = NablaMessagingClient.shared.send(
  messageInput,
  inConversationWithId: conversationId
) { result in
  switch result {
    case .success:
      print("Message successfully sent")
    case let .error(error):
      print("Error \(error)")
  }
}

You can send 4 types of messages:

  • Text
  • Image
  • Document
  • Audio

Handling failure

If sending the message fails, the message will still be included in watchConversationItems but its state will be ConversationItemState.failed. You can then retry sending it:

private var retrySendingMessageAction: Cancellable?

self.retrySendingMessageAction = NablaMessagingClient.shared.send(
  itemWithId: myMessage.id,
  inConversationWithId: conversationId
) { result in
  switch result {
    case .success:
      print("Message successfully sent")
    case let .error(error):
      print("Error \(error)")
  }
}

Did this page help you?