PrivMX DOCS
JavaScript

Messages

Sample code on this page is based on the initial assumptions.

Threads allow the exchange of messages. In this section, let's take a closer look at the structure of messages and how to send them. We will also discuss some good practice when working with Threads.

About Messages

The structure of a message and a brief description of its elements is outlined in the following table:

fieldtypeencrypteddescription
databinaryyescontent of the message
infoServerInfonoadditional information assigned by the server e.g. author, creationDate, messageID and threadID
privateMetabinaryyesadditional information about the message
publicMetabinarynoadditional public information about the message, also accessible through PrivMX Bridge API

Define structure

Thread's architecture does not require you to use a specific data structure inside the messages. So before working with Threads, define what what kind of messages you want to send.

We recommend future-proofing your messages right from the start, i.e. choosing an easily modifiable format.

It is also a good idea to include its type and version in the structure of the message.

Here is an example message structure that you can use in your project.

JSON
{
    “data”:{
        “content”: "string", // string / binary data containing for example: markdown, html or json
    },
        “info”: "ServerInfo", // assigned by server,
        “publicMeta”:{
        “version”:number,
        “type”: “text”, // some kind of enum describing type of message like “event”, “html”,“markdown” etc.
    },
        “privateMeta”:{
        // meta fields
    }
}

Remember that it is only an example and you should consider your app's requirements and limitations.

Serialization of Data

To send a message you have to use the Endpoint's sendMessage method. This method accepts arguments in Uint8Array.

Sending Messages

Example of sending a message in Plain Text:

JavaScript
const message = 'Message text';
 
const encoder = new TextEncoder();

await threadApi.sendMessage(
    threadId, 
    serializeObject("some public meta-data"), 
    serializeObject("some private meta-data"), 
    serializeObject(message)
);

Receiving Messages

To get a list of messages inside a Thread, use listMessages method. Because data inside messages is in Uint8Array you have to deserialize it to human-readable string. Endpoint takes care of encrypting your data before sending it to PrivMX Bridge.

JavaScript
const defaultListQuery = {skip: 0, limit: 100, sortOrder: "desc"};

const messages = await threadApi.listMessages(threadId, defaultListQuery);
const decodedMessages = messages.readItems.map(x =>
   {
      return {
        publicMeta: deserializedObject(x.publicMeta),
        privateMeta: deserializedObject(x.privateMeta),
        data: deserializedObject(x.data)
      }
   }
)
console.log("messages", messages);
console.log("messages in human-readable format", decodedMessages);

Deleting a Message

To delete a message, you need to use the deleteMessage method.

JavaScript
await threadApi.deleteMessage(messageId);

We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.

On this page

Messages | PrivMX Docs