Skip to main content
js

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.

Working with Threads

When working with Threads, you will use the following:

  • connection().threads - provides methods used to manage Threads in given Context;
  • connection().thread("THREAD_ID") - provides methods used to work with a specific Thread. This includes getting/updating info, deleting, and sending messages.

Creating Threads

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

const users = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }, { userId: 'MY_USER2', pubKey: 'PUB_KEY2' }];
const managers = [{ userId: 'MY_USER', pubKey: 'PUB_KEY' }];

const threadId = await Endpoint.connection().threads.new({
contextId: 'CONTEXT_ID',
users,
managers
});

Getting Threads

Fetching the most recent Threads in given Context:

const threadList = await Endpoint.connection().threads.list({
contextId: 'CONTEXT_ID'
});

const parsedThreads = threadList.readItems.map(thread => {
return {
...thread,
privateMeta: deserializeObject(thread.privateMeta),
publicMeta: deserializeObject(thread.publicMeta)
};
});

Remember that data is transmitted in binary format, so you'll need to parse it back into your desired format. You can use the deserializeObject function provided by the SDK for this purpose.

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).
const thread = Endpoint.connection().thread('THREAD_ID');
const threadInfo = await thread.info();

const newUsers = threadInfo.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 =>
threadInfo.managers.find(manager => manager == user.userId));

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

await thread.update({
publicMeta: threadInfo.publicMeta,
version: threadInfo.version,
users: newUsers,
managers: newManagers,
privateMeta: serializeObject(newPrivateMeta)
});