PrivMX DOCS
C++

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:

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.

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

Sending Messages

Sending a simple message to the given Thread:

C++
// ...

// send messages to the Thread represented by its threadId
threadApi.sendMessage(
    threadId, 
    core::Buffer::from("some public meta-data"), 
    core::Buffer::from("some private meta-data"), 
    core::Buffer::from("serialized message data")
);

// ...

Getting Messages

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

Reading the messages of the Thread (limited to the 100 newest entries as described by the defaultListQuery object):

C++
// ...

auto messages = threadApi.listMessages(threadId, defaultListQuery);
for (auto msg: messages.readItems) {
    std::cout << "message: " << msg.data.stdString()
        << " / public meta: " << msg.publicMeta.stdString() 
        << " / private meta: " << msg.privateMeta.stdString() 
        << std::endl;
}

// ...

Deleting a Message

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

C++
// ...
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