Skip to main content

Messages

Sending messages in Threads.

Info

The sample code on this page is based on the same assumptions mentioned in Fist App.

Messages Inside Threads

Messages inside Threads are sent in binary format. Before sending a message, you need to decide on the message format and choose the appropriate data serialization method.

For more information about the Threads architecture and best practices for sending messages, visit the Threads Documentation.

Serialization of Data

To send a message you have to use the Endpoint's sendMessage method. This method accepts arguments in Uint8Array, here are some of the ways you can serialize your JavaScript objects:

Sending Messages

Example of sending a message in Plain Text:

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

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

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