Skip to main content
js

Messages

Sending messages in Threads.

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:

Basic Example - JSON

A common way to serialize JS objects is to parse them to a JSON string using JSON.stringify function and then encode them using TextEncoder. Because it's so common, our SDK exports helper functions to serialize and deserialize objects this way.

import {deserializeObject,serializeObject} from "@simplito/privmx-endpoint-web-sdk"

const data = {
content: 'MESSAGE_CONTENT',
type: 'text'
};

const binaryData = serializeObject(data); // returns data encoded to Uint8Array
const decodedData = deserializeObject(binaryData); // returns JavaScript object

For simplicity, all the following examples will use this serialization method. Feel free to use the most appropriate one for your needs.

Using Binary Format

Depending on specific use cases, there may be more efficient formats than JSON. One of the alternatives is MessagePack. MessagePack offers smaller data sizes and faster performance.

Here is a simple example of sending a message in MessagePack format.

// First, install it using `npm install @msgpack/msgpack`
import { encode,decode } from "@msgpack/msgpack";

const data = {
content: "This is my message",
type:"text"
}

const binaryData = encode(data);
const decodedData = decode(binaryData)

Sending Messages

Example of sending a message in Plain Text:

const thread = Endpoint.connection().thread('THREAD_ID');
const message = 'Message text';

const encoder = new TextEncoder();
const messageId = await thread.sendMessage({
data: encoder.encode(message)
});

const messageList = await thread.getMessages();

const decoder = new TextDecoder();
const parsedMessages = messageList.readItems.map(message => {
return {
...message,
data: decoder.decode(message.data)
};
});

Example: Responding to Other Messages

The snippet below shows how to set up a feature allowing users to respond to messages.

const thread = Endpoint.connection().thread('THREAD_ID');

// responseTo is the ID of the message you want to respond to
const publicMeta = { responseTo: 'MESSAGE_ID_TO_RESPOND' };

const message = {
content: 'Response to message',
type: 'text'
};


const msgId = await thread.sendMessage({
data: serializeObject(message),
publicMeta: serializeObject(publicMeta)
});

const messageList = await thread.getMessages();
const parsedMessages = messageList.readItems.map(message => {
const responseTo = deserializeObject(message.publicMeta).responseTo;

return {
...message,
data: deserializeObject(message.data),
responseToId: responseTo || null
};
});