import ballerina/io;
import ballerinax/stan;
// Produces a message to a subject in the NATS streaming sever.
public function main() returns error? {
string message = "Hello from Ballerina";
// Initializes the NATS Streaming client with TLS/SSL and username/password authentication.
stan:Client stanClient = check new(stan:DEFAULT_URL,
clusterId = "my_secure_cluster",
// To secure the client connections using username/password authentication, provide the credentials
// with the [`stan:Credentials`](https://docs.central.ballerina.io/ballerinax/stan/latest/records/Credentials) record.
auth = {
username: "alice",
password: "alice@123"
},
// To secure the client connection using TLS/SSL, the client needs to be configured with
// a certificate file of the server.
// The [`stan:SecureSocket`](https://docs.central.ballerina.io/ballerinax/stan/latest/records/SecureSocket)
// record provides the SSL-related configurations of the client.
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
// Produces a message to the specified subject.
string result = check stanClient->publishMessage({
content: message.toBytes(),
subject: "demo"});
io:println("GUID " + result + " received for the produced message.");
// Closes the client connection.
check stanClient.close();
}
import ballerina/log;
import ballerinax/stan;
// Initializes the NATS Streaming listener with TLS/SSL and username/password authentication.
listener stan:Listener securedEP = new(stan:DEFAULT_URL,
clusterId = "my_secure_cluster",
// To secure the client connections using username/password authentication, provide the credentials
// with the [`stan:Credentials`](https://docs.central.ballerina.io/ballerinax/stan/latest/records/Credentials) record.
auth = {
username: "alice",
password: "alice@123"
},
// To secure the client connection using TLS/SSL, the client needs to be configured with
// a certificate file of the server.
// The [`stan:SecureSocket`](https://docs.central.ballerina.io/ballerinax/stan/latest/records/SecureSocket)
// record provides the SSL-related configurations of the client.
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
// Binds the consumer to listen to the messages published to the 'security.demo' subject.
@stan:ServiceConfig {
subject: "security.demo"
}
service stan:Service on securedEP {
remote function onMessage(stan:Message message) {
// Prints the incoming message in the console.
string|error messageData = string:fromBytes(message.content);
if messageData is string {
log:printInfo("Received message: " + messageData);
}
}
}
Secured connectionIn this example, the underlying connections of the subscriber and the publisher are
secured with TLS/SSL and basic authentication. |
import ballerina/io;
import ballerinax/stan;
public function main() returns error? {
string message = "Hello from Ballerina";
Produces a message to a subject in the NATS streaming sever.
stan:Client stanClient = check new(stan:DEFAULT_URL,
clusterId = "my_secure_cluster",
Initializes the NATS Streaming client with TLS/SSL and username/password authentication.
auth = {
username: "alice",
password: "alice@123"
},
To secure the client connections using username/password authentication, provide the credentials
with the stan:Credentials
record.
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
To secure the client connection using TLS/SSL, the client needs to be configured with
a certificate file of the server.
The stan:SecureSocket
record provides the SSL-related configurations of the client.
string result = check stanClient->publishMessage({
content: message.toBytes(),
subject: "demo"});
io:println("GUID " + result + " received for the produced message.");
Produces a message to the specified subject.
check stanClient.close();
}
Closes the client connection.
bal run publisher.bal
GUID m2jS6SLLefK325DWTkkwBh received for the produced message.
import ballerina/log;
import ballerinax/stan;
listener stan:Listener securedEP = new(stan:DEFAULT_URL,
clusterId = "my_secure_cluster",
Initializes the NATS Streaming listener with TLS/SSL and username/password authentication.
auth = {
username: "alice",
password: "alice@123"
},
To secure the client connections using username/password authentication, provide the credentials
with the stan:Credentials
record.
secureSocket = {
cert: "../resource/path/to/public.crt"
}
);
To secure the client connection using TLS/SSL, the client needs to be configured with
a certificate file of the server.
The stan:SecureSocket
record provides the SSL-related configurations of the client.
@stan:ServiceConfig {
subject: "security.demo"
}
service stan:Service on securedEP {
remote function onMessage(stan:Message message) {
Binds the consumer to listen to the messages published to the ‘security.demo’ subject.
string|error messageData = string:fromBytes(message.content);
if messageData is string {
log:printInfo("Received message: " + messageData);
}
}
}
Prints the incoming message in the console.
bal run subscriber.bal
time = 2021-05-20T12:51:47.417+05:30 level = INFO module = "" message = "Received message: Hello from Ballerina"