import ballerinax/nats;

// Produces a message to a subject in the NATS sever.
public function main() returns error? {
    string message = "Hello from Ballerina";
    // Initializes a client.
    nats:Client natsClient = check new;
    // Produces a message to the specified subject.
    check natsClient->publishMessage({
                             content: <@untainted>message.toBytes(),
                             subject: "demo.bbe.subject"});

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

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

// Binds the consumer to listen to the messages published to the 'demo' subject.
@nats:ServiceConfig {
    subject: "demo.bbe.*"
}
service nats:Service on subscription {

    remote function onMessage(nats:Message message) {
        // Prints the incoming message in the console.
        string|error messageContent = string:fromBytes(message.content);
        if (messageContent is string) {
            io:println("Received message: " + messageContent);
        }
    }
}

Basic Publisher and Subscriber

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.

For more information on the underlying module, see the NATS module.

import ballerinax/nats;
public function main() returns error? {
    string message = "Hello from Ballerina";

Produces a message to a subject in the NATS sever.

    nats:Client natsClient = check new;

Initializes a client.

    check natsClient->publishMessage({
                             content: <@untainted>message.toBytes(),
                             subject: "demo.bbe.subject"});

Produces a message to the specified subject.

    check natsClient.close();
}

Closes the client connection.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `bal run` command below.
bal run publisher.bal
import ballerina/io;
import ballerinax/nats;
listener nats:Listener subscription = new;

Initializes the NATS listener.

@nats:ServiceConfig {
    subject: "demo.bbe.*"
}
service nats:Service on subscription {

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

    remote function onMessage(nats:Message message) {
        string|error messageContent = string:fromBytes(message.content);
        if (messageContent is string) {
            io:println("Received message: " + messageContent);
        }
    }
}

Prints the incoming message in the console.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `bal run` command below.
bal run subscriber.bal
[ballerinax/nats] Client subscribed for subject demo.bbe.*
Received message: Hello from Ballerina