Skip to main content

Overview

Threads are a secure way for assigned members to exchange encrypted messages.

Info

Before working with Threads, 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 same assumptions mentioned in Fist App.

Working with Threads

When working with Threads, you will use the following:

  • ThreadApi - provides methods used to manage Threads in given Context
const threadApi  = await Endpoint.createThreadApi(connection);

Creating Threads

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

    const managers = [
{userId: USER1_ID, pubKey: USER1_PUBLIC_KEY}
];

const users = [
{userId: USER1_ID, pubKey: USER1_PUBLIC_KEY},
{userId: USER2_ID, pubKey: USER2_PUBLIC_KEY}
];

// create a new Thread with access for user_1 as manager and user_2 as regular user
const threadId = await threadsApi.createThread(CONTEXT_ID, users, managers,
serializeObject("some thread's public meta-data"),
serializeObject("some thread's private meta-data")
);

Fetching Threads

Fetching the most recent Threads in given Context:

const defaultListQuery = {skip: 0, limit: 100, sortOrder: "desc"};
const threadList = await threadApi.listThreads(CONTEXT_ID, defaultListQuery);

As a result you will receive an object:

// threadList:
{
readItems: [<threadObject1>, <threadObject2>,..., <threadObjectN>],
totalAvailable: <number_of_all_threads>
}

A detailed description of the Thread object fields can be found here.

Modifying Threads

How Updates Work

To update a Thread you must always provide a full list of parameters.

The updateThread(...) method needs all the parameters as in the createThread(...) method and a few more. If we want to update one of the parameters - we provide it in a new modified form. If - on the other hand - we want to leave the parameter unchanged, we provide it as it was before. All current values ​​of the parameters of a given Thread can be obtained using getThread(threadId)


const thread = await threadApi.getThread(
threadId
);

const newUsers = thread.users.map(user => ({
//Your application must provide a way,
//to get user's public key from their userId.
userId: user,
pubKey: '<USER_PUBLIC_KEY>'
})
);

const newManagers = newUsers.filter(user =>
thread.managers.find(manager => manager == user.userId));

const newPrivateMeta = {
title: 'New thread name'
};

await threadApi.updateThread(
threadId
newUsers,
newManagers,
thread.publicMeta,
serializeObject(newPrivateMeta)
thread.version,
false,
false
);