This package provides the capability to send and receive messages by connecting to the NATS server.
NATS messaging enables the communication of data that is segmented into messages among computer applications and services. Data is encoded and framed as a message and sent by a publisher. The message is received, decoded, and processed by one or more subscribers. NATS makes it easy for programs to communicate across different environments, languages, cloud providers, and on-premise systems. Clients connect to the NATS system usually via a single URL and then subscribe or publish messages to a subject.
First, you need to set up the connection with the NATS Basic server. The following ways can be used to connect to a NATS Basic server.
1nats:Client natsClient = check new(nats:DEFAULT_URL);
1nats:Client natsClient = check new("nats://serverone:4222");
1nats:ConnectionConfiguration config = {2 connectionName: "my-nats",3 noEcho: true4};5nats:Client natsClient = check new(["nats://serverone:4222", "nats://servertwo:4222"], config);
Once connected, publishing is accomplished via one of the three methods below.
1string message = "hello world";2nats:Error? result =3 natsClient->publishMessage({ content: message.toBytes(), subject: "demo.nats.basic"});
1string message = "hello world";2nats:Message|nats:Error reqReply =3 natsClient->requestMessage({ content: message.toBytes(), subject: "demo.nats.basic"}, 5);
replyTo
subject:1string message = "hello world";2nats:Error? result = natsClient->publish({ content: message.toBytes(), subject: "demo.nats.basic",3 replyTo: "demo.reply" });
onMessage
remote method:1// Binds the consumer to listen to the messages published to the 'demo.example.*' subject2@nats:ServiceConfig {3 subject: "demo.example.*"4}5service nats:Service on new nats:Listener(nats:DEFAULT_URL) {67 remote function onMessage(nats:Message message) {8 }9}
onRequest
remote method:1// Binds the consumer to listen to the messages published to the 'demo.example.*' subject2@nats:ServiceConfig {3 subject: "demo.example.*"4}5service nats:Service on new nats:Listener(nats:DEFAULT_URL) {67 // The returned message will be published to the replyTo subject of the consumed message8 remote function onRequest(nats:Message message) returns string? {9 return "Reply Message";10 }11}
The Ballerina NATS package allows the use of TLS in communication. This setting expects a secure socket to be set in the connection configuration as shown below.
nats:Listener
1nats:SecureSocket secured = {2 cert: {3 path: "<path>/truststore.p12",4 password: "password"5 },6 key: {7 path: "<path>/keystore.p12",8 password: "password"9 }10};11nats:Listener natsListener = check new("nats://serverone:4222", secureSocket = secured);
nats:Client
1nats:SecureSocket secured = {2 cert: {3 path: "<path>/truststore.p12",4 password: "password"5 },6 key: {7 path: "<path>/keystore.p12",8 password: "password"9 }10};11nats:Client natsClient = check new("nats://serverone:4222", secureSocket = secured);
To report bugs, request new features, start new discussions, view project boards, etc., go to the Ballerina standard library parent repository.
ballerinax/nats
sha-256:
e660a0913da61511949ef517171ff48867d10279ee5aa881c45eb0083decc12d
License: Apache-2.0
Created date: 01 June,2023
Platform: java11
Ballerina version: 2201.5.0
service
client
messaging
network
pubsub