Skip to main content
swift

Overview

Threads are a secure way for assigned members to exchange encrypted messages. Before working with Threads, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.

Info

Thread 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.

Working with Threads

To access Threads methods, get the field threadApi from active connection. Connection should be initialized with [.thread] and passed to PrivmxEndpoint.

let connectionID = try? await endpointContainer.newEndpoint(enabling: [.thread], connectingAs: privateKey, to: solutionId, on: platformURL)
guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
privMXEndpoint.threadApi? // instance of ThreadApi

Creating Threads

Creating a basic, unnamed Thread, which can act as an encrypted data container.

guard let privMXEndpoint =  endpointContainer.getEndpoint(endpointId) else {return}


let users = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let managers = [privmx.endpoint.core.UserWithPubKey]() //should be prepared by developer
let publicMeta = Data()
let privateMeta = Data()

try? privMXEndpoint.threadApi?.createThread(
in: contextId,
for: users,
managedBy: managers,
withPublicMeta: publicMeta,
withPrivateMeta: privateMeta)

Getting Threads

Fetching the most recent Threads in given Context:

var startIndex:Int64 = 0
var pageSize:Int64 = 100

guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}
guard let pagingList = try? privMXEndpoint.threadApi?
.listThreads(
from:contextId,
basedOn: .init(skip: startIndex, limit: pageSize, sortOrder: .desc)) else {return}

let threads = pagingList.readItems.map { $0 }

Managing Threads

To update a Thread you must always provide its current version, as well as:

  • list of users
  • list of managers
  • new private and public meta (even if it didn't change)
  • Thread's current version
  • true if update action should be forced
struct ThreadPublicMeta:Codable{
let tags: [String]
}
var threadID = "THREAD_ID"

guard let privMXEndpoint = endpointContainer.getEndpoint(endpointId) else {return}

let threadPublicMeta = ThreadPublicMeta(tags: ["TAG1","TAG2","TAG3"])
guard let publicMeta = try? JSONEncoder().encode(threadPublicMeta) else {return}
guard let thread = try? privMXEndpoint.threadApi?
.getThread(threadID) else {return}

//users list to be extracted from thread
let users = thread.users.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}
//managers list to be extracted from thread
let managers = thread.managers.map{
//Your application must provide a way,
//to get user's public key from their userId.
privmx.endpoint.core.UserWithPubKey(userId: $0, pubKey: "PUB")
}

guard let newPrivateMeta = "New thread name".data(using: .utf8) else {return}

_ = try? privMXEndpoint.threadApi?.updateThread(
threadID,
atVersion: thread.version,
replacingUsers: users,
replacingManagers: managers,
replacingPublicMeta: thread.publicMeta.getData() ?? Data(),
replacingPrivateMeta: newPrivateMeta,
force:false,
forceGenerateNewKey: false)