Message management


Let's review some code examples on how to handle messages with the Java client library:

Tip: if you're new to our Java library, we suggest you begin with our Get Started guide.

Sending messages

Immutable data can be stored on the ledger. The data can be encrypted so that only selected identities can read the data.

To send the encrypted string Hello which only the sender and recipient can read:

Result result = api.sendMessage(<to-address>, "Hello".getBytes(StandardCharsets.UTF_8), true);

To send the unencrypted string Hello:

Result result = api.sendMessage(<to-address>, "Hello".getBytes(StandardCharsets.UTF_8), false);

Or equivalently,

SendMessageAction msgAction = SendMessageAction.create(api.getAddress(), <to-address>, "Hello".getBytes(StandardCharset.UTF_8), false);
Result result = api.execute(msgAction);

Receiving messages

To read (and decrypt if necessary) all the readable data at an address:

Observable<DecryptedMessage> readable = api.getMessages();
readable.subscribe(data -> { ... });

Note: data that can't be decrypted by the user's key is simply ignored.

Messaging example

Based on the previous sending and receiving samples, the following code snippet presents a full messaging example.

Note: you can get the complete sample source code from our GitHub repository.

public class MessagesExample {
public static void main(String[] args) {
// Create a new public key identity
final RadixIdentity radixIdentity = RadixIdentities.createNew();
// Initialize api layer
RadixApplicationAPI api = RadixApplicationAPI.create(Bootstrap.LOCALHOST, radixIdentity);
// Sync with network
System.out.println("My address: " + api.getAddress());
System.out.println("My public key: " + api.getPublicKey());
// Print out all past and future messages
// Send a message to an address
RadixAddress toAddress = RadixAddress.from("JEbhKQzBn4qJzWJFBbaPioA2GTeaQhuUjYWkanTE6N8VvvPpvM8");
Result result = api.sendMessage(toAddress, "Hello".getBytes(RadixConstants.STANDARD_CHARSET), true);