Skip to main content
java

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 kotlinx-serialization-json-jvm dependency for Kotlin and com.google.code.gson:gson for Java to serialize object to JSON format.

Working with Threads

To access Threads methods, get the field threadApi from active connection. Connection should be initialized with Modules.THREAD and passed to PrivmxEndpoint().

ThreadApi threadApi = connection.threadApi;

Creating Threads

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

val contextId : String = "CONTEXT_ID"
val users : List<UserWithPubKey> = listOf() // users to add to the Thread
val managers : List<UserWithPubKey> = listOf() // managers to add to the Thread
val threadApi = connection.threadApi

val threadId = threadApi.createThread(
contextId,
users,
managers,
ByteArray(0),
ByteArray(0)
)

Getting Threads

Define Thread item class with decoded publicMeta and privateMeta:

data class ThreadItem(
val thread: com.simplito.java.privmx_endpoint.model.Thread,
val decodedPrivateMeta: String,
val decodedPublicMeta: ThreadPublicMeta
)

Fetching the most recent Threads in given Context:

val contextId : String = "CONTEXT_ID"
val startIndex = 0L
val pageSize = 100L
val threadApi = connection.threadApi

val threadsPagingList = threadApi.listThreads(
contextId,
startIndex,
pageSize,
SortOrder.DESC
)

val threads = threadsPagingList.readItems.map {
ThreadItem(
it,
it.privateMeta.decodeToString(),
Json.decodeFromString(it.publicMeta.decodeToString())
)
}

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
val threadID = "THREAD_ID"
val thread: Thread = connection.threadApi.getThread(threadID)
val users = thread
.users
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val managers = thread
.managers
.map { userId ->
//Your application must provide a way,
//to get user's public key from their userId.
UserWithPubKey(
userId,
"USER_PUBLIC_KEY"
)
}
val newThreadNameAsPrivateMeta = "New thread name"
val threadApi = connection.threadApi

threadApi.updateThread(
thread.threadId,
users,
managers,
thread.publicMeta,
newThreadNameAsPrivateMeta.encodeToByteArray(),
thread.version,
false
)