Messages
Message functionality is wrapped in the SimpleMessagesProtocol
object. It provides methods to send and receive messages of different types. It also provides overrides for their Multicast
couterparts. Last but not the least, there are methods available to Publish
messages on topics and send them to all subscribers.
The following types of messages are supported:
- Text
- Binary
- Byte Array
- ByteString (protobuf)
- Object
- Ack
#
Receiving MessagesThe following options are available for receiving messages
#
Message ListenerSet a message listener which accepts a consumer which is called when a new message is received.
client.simpleMessagesProtocol().onNewMessage(receivedMessage -> { if (receivedMessage.isText) { System.out.println("New text message from " + receivedMessage.from); System.out.println(receivedMessage.textData); } else if (receivedMessage.isBinary) { System.out.println("New binary message from " + receivedMessage.from); System.out.println(Hex.toHexString(receivedMessage.binaryData.toByteArray()).toUpperCase()); }});
#
Message Listener With ReplyIn order to send a reply corresponding to an incoming message, use the onNewMessageWithReply
method. This method expects a function with input as a ReceivedMessage
and output as an Object
client.simpleMessagesProtocol().onNewMessageWithReply(receivedMessage -> { if (receivedMessage.isText) { System.out.println("New text message from " + receivedMessage.from); System.out.println(receivedMessage.textData); } else if (receivedMessage.isBinary) { System.out.println("New binary message from " + receivedMessage.from); System.out.println(Hex.toHexString(receivedMessage.binaryData.toByteArray()).toUpperCase()); } return "Reply message";});
#
Sending MessagesThere are many overloaded methods available for sending messages. Each option has a text and binary counterpart available.
#
Send TextThe sendTextAsync
method and its overloaded counterparts can be used to send text messages asynchronously across the network.
Identity identity = new Identity("server", Wallet.createNew());
NKNClient nknClient = new NKNClient(identity);nknClient.start();
CompletableFuture<SimpleMessagesProtocol.ReceivedMessage> promise = nknClient.simpleMessagesProtocol().sendTextAsync(identity.getFullIdentifier(), "Hello there!");
promise.whenComplete((response, error) -> { if (error == null) { System.out.println("Response ==> " + response.textData); } else { error.printStackTrace(); }});
#
Send BinarySimilar to its send text cousin, the sendBinaryAsync
method and its overloaded counterparts can be used to send binary messages across the network.
Identity identity = new Identity("server", Wallet.createNew());
NKNClient nknClient = new NKNClient(identity);nknClient.start();
CompletableFuture<SimpleMessagesProtocol.ReceivedMessage> promise = nknClient.simpleMessagesProtocol().sendBinaryAsync(identity.getFullIdentifier(), "Hello there!".getBytes(StandardCharsets.UTF_8));
promise.whenComplete((response, error) -> { if (error == null) { System.out.println("Response ==> " + response.textData); } else { error.printStackTrace(); }});
info
The sendBinaryAsync
method also accepts ByteString
as an argument instead of the byte[]
.
#
Send ObjectSend any java object across the wire using the sendAsync
method
Identity identity = new Identity("server", Wallet.createNew());
NKNClient nknClient = new NKNClient(identity);nknClient.start();
CompletableFuture<SimpleMessagesProtocol.ReceivedMessage> promise = nknClient.simpleMessagesProtocol().sendAsync(identity.getFullIdentifier(), null, new Object());
promise.whenComplete((response, error) -> { if (error == null) { System.out.println("Response ==> " + response.textData); } else { error.printStackTrace(); }});
#
Send Multicast TextThe sendTextMulticastAsync
can be used to send a multicast text message across the network. This method is similar to sendTextAsync
but accepts an array of destination identifiers instead.
List<CompletableFuture<SimpleMessagesProtocol.ReceivedMessage>> promises = nknClient.simpleMessagesProtocol().sendTextMulticastAsync(new String[] {identity.getFullIdentifier()}, "Hello to everyone!!");
promises.forEach(p -> p.whenComplete((response, error) -> { if (error == null) { System.out.println("Response from " + response.from); } else { System.out.println("Error: " + error.toString()); }}));
#
Send Multicast BinaryThe sendMulticastBinaryAsync
method can be used to send a multicast binary message across the network.
List<CompletableFuture<SimpleMessagesProtocol.ReceivedMessage>> promises = nknClient.simpleMessagesProtocol().sendBinaryMulticastAsync(new String[] {identity.getFullIdentifier()}, "Hello to everyone!!".getBytes());
promises.forEach(p -> p.whenComplete((response, error) -> { if (error == null) { System.out.println("Response from " + response.from); } else { System.out.println("Error: " + error.toString()); }}));
#
Send Multicast ObjectThe sendMulticastAsync
method can be used to send a multicast object message across the network.
List<CompletableFuture<SimpleMessagesProtocol.ReceivedMessage>> promises = nknClient.simpleMessagesProtocol().sendBinaryMulticastAsync(new String[] {identity.getFullIdentifier()}, new Object());
promises.forEach(p -> p.whenComplete((response, error) -> { if (error == null) { System.out.println("Response from " + response.from); } else { System.out.println("Error: " + error.toString()); }}));
#
Publish TextPublish a text message to all subscribers of a topic using the publishTextAsync
method.
List<CompletableFuture<SimpleMessagesProtocol.ReceivedMessage>> promises = nknClient.simpleMessagesProtocol().publishTextAsync("topicNAme", false, "Hello Subscribers!!");
note
The publishBinaryAsync
method is the binary counterpart for publishing binary
and byteString
message types to a topic.
#
Miscellaneous#
Toggle Auto AckStop sending automatic ack messages. An Ack message does not have any data. It is only used to inform a sender of the message that the receiving party has received the message successfully.
client.simpleMessagesProtocol().setNoAutomaticACKs(true);