Skip to main content

Work in progress

Swift is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however

Messages

Sending messages in Threads.

Info

Before working with messages, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.

The sample code on this page is based on the initial assumptions.

All policies in the following examples can be build with Policy Builders, according to Policies overview.

Message data, publicMeta and privateMeta fields support any kind of data formats encoded to byte arrays. Examples in this section use JSON format serialization which is available directly in Swift.

Messages inside Threads

Messages inside Threads are sent in binary format. Before sending a message, you need to decide on the message format and choose the appropriate data serialization method.

For more information about the Threads architecture and best practices for sending messages, visit the Threads Documentation.

Sending Messages

Example of sending a message in Plain Text:

let threadId = "SOME_THREAD_ID"
guard let messageData = "".data(using: .utf8) else {return}
// In this example, both publicMeta and privateMeta are empty.
let messageID = try? endpointSession?.threadApi?.sendMessage(
in: threadId,
withPublicMeta: Data(),
withPrivateMeta: Data(),
containing: messageData)

Getting Messages

Define message item struct with decoded data and publicMeta. This time we use some decoding of Data fields and mapping generic message to struct used within developed App:

struct MessagePublicMeta:Codable{
let responseTo: String
}
struct MessageData:Codable{
let content: String
let type: String
}
struct MessageItem:Codable{
let messageId: String
let publicMeta: MessagePublicMeta?
let data: MessageData?
}

Fetching the most recent messages in given Thread:

let threadId = "THREAD_ID"
let startIndex:Int64 = 0
let pageSize:Int64 = 100

guard let pagingList = try? endpointSession?.threadApi?
.listMessages(
from:threadId,
basedOn: .init(skip: startIndex, limit: pageSize, sortOrder: .desc)) else {return}

let messages =
pagingList.readItems.map {

MessageItem(messageId: $0.id,
publicMeta: try? JSONDecoder().decode(MessagePublicMeta.self, from: $0.publicMeta.getData() ?? Data()),
data: try? JSONDecoder().decode(MessageData.self, from: $0.data.getData() ?? Data()))

}

Managing Messages

Example of updating the message content:

var messageId = "MESSAGE_ID"

guard let message = try? endpointSession?.threadApi?
.getMessage(messageId) else {return}
let newMessage = MessageData(content: "Hello World", type: "text")
guard let newMessageData = try? JSONEncoder().encode(newMessage) else {return}
guard let privateMeta = message.privateMeta.getData() else {return}
guard let publicMeta = message.publicMeta.getData() else {return}

try? endpointSession?.threadApi?
.updateMessage(message.id,
replacingData: newMessageData ,
replacingPublicMeta: privateMeta,
replacingPrivateMeta: publicMeta)