Sending Messages
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:
field | type | encrypted | description |
---|---|---|---|
data | binary | yes | content of the message |
info | ServerInfo | no | additional information assigned by the server e.g. author, creationDate , messageID and threadID |
privateMeta | binary | yes | additional information about the message |
publicMeta | binary | no | additional public information about the message, also accessible through Platform REST API |
To learn more about how to use publicMeta in your project, browse public/privateMeta use-cases.
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.
{
“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.
Sending
With the structure ready, its time to start sending messages.
Info
Remember that before sending anything, you have to be connected to the Platform - using platformConnect
.
const msgId = await Endpoint.connection().thread('THREAD_ID').sendMessage({
data: new TextEncoder().encode('My message'),
privateMeta: new TextEncoder().encode('My private data'),
publicMeta: serializeObject({
type: 'type_of_message'
})
});
For JavaScript reference go here.
After sending the message, you will get its ID returned.
Message IDs are unique within a Context, allowing you to locate a message solely by its ID, without the need to know the associated Thread ID.