Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
NATS JetStream client - Publish message
The nats:JetStreamClient
allows you to publish messages to a specific subject. To create a nats:JetStreamClient
, you need to provide a valid instance of the nats:Client
. Before publishing messages, you should call the addStream
function to configure the stream. This function requires a nats:JetStreamConfiguration
object with the name
, subjects
, and storageType
values. To publish messages, you can use the publishMessage
method, which takes the message content and subject as arguments. This method allows you to send messages that can be received by one or more subscribers.
import ballerina/http;
import ballerinax/nats;
type Order readonly & record {
int orderId;
string productName;
decimal price;
boolean isValid;
};
service / on new http:Listener(9092) {
private final string SUBJECT_NAME = "orders";
private final nats:JetStreamClient orderClient;
function init() returns error? {
// Initiate a NATS client passing the URL of the NATS broker.
nats:Client natsClient = check new (nats:DEFAULT_URL);
// Initiate the NATS `JetStreamClient` at the start of the service. This will be used
// throughout the lifetime of the service.
self.orderClient = check new (natsClient);
nats:StreamConfiguration config = {
name: "demo",
subjects: [self.SUBJECT_NAME],
storageType: nats:MEMORY
};
_ = check self.orderClient->addStream(config);
}
resource function post orders(Order newOrder) returns http:Accepted|error {
// Produce a message to the specified subject.
check self.orderClient->publishMessage({
subject: self.SUBJECT_NAME,
content: newOrder.toString().toBytes()
});
return http:ACCEPTED;
}
}
Prerequisites
- • Start an instance of the NATS JetStream server.
- • Run the NATS JetStream service given in the NATS JetStream service - Consume message example.
Run the client program by executing the following command.
$ bal run nats_jetstream_pub.bal
Invoke the service by executing the following cURL command in a new terminal.
$ curl http://localhost:9092/orders -H "Content-type:application/json" -d "{\"orderId\": 1, \"productName\": \"Sport shoe\", \"price\": 27.5, \"isValid\": true}"
Related links
PreviousSend request message
NextSSL/TLS