import ballerina/io;
import ballerinax/nats;

public function main() returns error? {
    string message = "Hello from Ballerina";
    // Initializes a NATS client.
    nats:Client natsClient = check new(nats:DEFAULT_URL);

    // Sends a request and returns the reply.
    nats:Message reply = check natsClient->requestMessage({
                             content: message.toBytes(),
                             subject: "demo.bbe"});

    // Prints the reply message.
    string replyContent = check string:fromBytes(reply.content);
    io:println("Reply message: " + replyContent);

    // Closes the client connection.
    check natsClient.close();
}
import ballerina/log;
import ballerinax/nats;

// Initializes the NATS listener.
listener nats:Listener subscription = new(nats:DEFAULT_URL);

// Binds the consumer to listen to the messages published
// to the 'demo.bbe' subject.
service "demo.bbe" on subscription {

    remote function onRequest(nats:Message message) returns string {

        // Logs the incoming message.
        string|error messageContent = string:fromBytes(message.content);
        if (messageContent is string) {
            log:printInfo("Received message: " + messageContent);
        }

        // Sends the reply message to the `replyTo` subject
        // of the received message.
        return "Hello Back!";
    }
}

Request/Reply

The NATS client is used either to produce a message to a subject or consume a message from a subject. In order to execute this example, it is required that a NATS server is up and running on its default host, port, and cluster. For instructions on installing the NATS server, go to NATS Server Installation.

This is a simple request/reply messaging pattern example. For more information on the underlying module, see the nats module.

import ballerina/io;
import ballerinax/nats;
public function main() returns error? {
    string message = "Hello from Ballerina";
    nats:Client natsClient = check new(nats:DEFAULT_URL);

Initializes a NATS client.

    nats:Message reply = check natsClient->requestMessage({
                             content: message.toBytes(),
                             subject: "demo.bbe"});

Sends a request and returns the reply.

    string replyContent = check string:fromBytes(reply.content);
    io:println("Reply message: " + replyContent);

Prints the reply message.

    check natsClient.close();
}

Closes the client connection.

bal run publisher.bal
Reply message: Hello Back!
import ballerina/log;
import ballerinax/nats;
listener nats:Listener subscription = new(nats:DEFAULT_URL);

Initializes the NATS listener.

service "demo.bbe" on subscription {

Binds the consumer to listen to the messages published to the ‘demo.bbe’ subject.

    remote function onRequest(nats:Message message) returns string {
        string|error messageContent = string:fromBytes(message.content);
        if (messageContent is string) {
            log:printInfo("Received message: " + messageContent);
        }

Logs the incoming message.

        return "Hello Back!";
    }
}

Sends the reply message to the replyTo subject of the received message.

bal run subscriber.bal
[ballerinax/nats] Client subscribed for subject demo.bbe
time = 2021-05-19T10:14:09.200+05:30 level = INFO module = "" message = "Received message: Hello from Ballerina"