SippoClient

This section explains how to create a Chat and use chat actions

First step

Chat session has to be started

// Set delegate before to receive incoming events, if delegate is set after start session some events could be lost
let delegate = ChatsDelegateExample()
let chats = try? SippoClient.client.chats.get()
chats?.delegate = delegate

chats?.connect {
    // Chats actions are available now
}

Create a new OneToOneChat

let chats = try? SippoClient.client.chats.get()

chats?.createChat(creationType: .oneToOne("anUser")) { result in
    switch result {
    case let .success(chat):
        print("A new chat: \(chat)")
    case let .failure(error):
        print("Error creating the chat: \(error)")
    }
}

Create a new GroupChat

let chats = try? SippoClient.client.chats.get()

chats.createChat(.groupChat(named: "aGroupChat", participants: ["participant@domain"])) { result in
    switch result {
    case let .success(chat):
        print("A new chat: \(chat)")
    case let .failure(error):
        print("Error creating the chat: \(error)")
    }
}

Receive new chats

Implement a new QNChatsDelegate

class ChatsDelegateExample: QNChatsDelegate {

    func didReceive(_ chat: QNChatRepository) {
        // New Chat receive
        print("New Chat: \(chat)")
        // Message which starts the chat
        print("With message: \(chat.lastMessage)")
    }        
}

And set it to Chats

let delegate = ChatsDelegateExample()
try? SippoClient.client.chats.get().delegate = delegate

Send messages

Implement a QNChatDelegate

class ChatDelegate: QNChatDelegate {

    func didReceiveMessage(_ message: QNChatMessage) {

    }

    func didUpdateLastMessage(_ message: QNChatMessage) {

    }

    func didReceiveMessageStatusUpdate(_ message: QNChatStatusMessage) {

    }

    func didReceiveParticipantAction(_ action: QNChatMessage.ParticipantAction, from participant: QNChatParticipant) {

    }

    func didUpdateUnreadMessagesCount(_ unreadMessagesCount: Int) {

    }
}
}

Send a text or file message

var aChat: QNChatRepository!
let delegate = ChatDelegate()
aChat.delegate = delegate

// Text message
let textMessage = aChat.send(message: "This is a message")
// Message status can be checked from: 
// func didReceiveMessageStatusUpdate(_ message: QNChatStatusMessage) { 
    print("Trying to send: \(message.messageId)")
} delegate function

// File message
let fileMessage = aChat.send(file: Data(), named: "photo.png")
// This callback will be call everytime the upload changes (bytes sent, status...)
fileMessage.fileUploadUpdates { update in
    print("Update progress: \(update?.bytesSent)")
}

Mark a message

var aChat: QNChatRepository!
var receivedMessage: QNChatMessage

aChat.mark(message: receivedMessage, as: .displayed)

Send participant action

var aChat: QNChatRepository!

aChat.sendParticipantActionInChat(.typing)

Receive messages

Stored messages

var aChat: QNChatRepository!
let delegate = ChatDelegate()
aChat.delegate = delegate

// Use search to fetch message from history
aChat.fetchMessagesFromStore(messageSearch: QNChatMessageSearch(limitTo: 20)) { messagesArray in

}

Messages from other participant

var aChat: QNChatRepository!
let delegate = ChatDelegate()
aChat.delegate = delegate

// Receive text or file messages from:
// func didReceiveMessage(_ message: QNChatMessage) delegate function

// Receive status messages from:
// func didReceiveMessageStatusUpdate(_ message: QNChatStatusMessage) delegate function

// Receive participant actions (typing, active) from:
// func didReceiveParticipantAction(_ action: QNChatMessage.ParticipantAction, from participant: QNChatParticipant) delegate function