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
- 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
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
MQTT service - SSL/TLS
The mqtt:Service
receives messages from the MQTT server using the mqtt:Listener
via SSL/TLS. SSL/TLS can be enabled by configuring the secureSocket
, which requires a certificate or a truststore. Further, Mutual TLS (mTLS) can be enabled by providing a certificate and private key of the service or a keystore. Use this to connect to an MQTT server secured with SSL.
import ballerina/lang.value;
import ballerina/log;
import ballerina/mqtt;
import ballerina/time;
type TemperatureDetails readonly & record {
string deviceId;
time:Utc timestamp;
decimal temperature;
};
listener mqtt:Listener temperatureSubscriber = new ("ssl://localhost:8883", "temperature-sub-client", "mqtt/topic", {
connectionConfig: {
// Provide the relevant secure socket configurations by using `mqtt:SecureSocket`.
secureSocket: {
cert: "./resources/path/to/public.crt"
}
}
});
service on temperatureSubscriber {
remote function onMessage(mqtt:Message message) returns error? {
TemperatureDetails details = check value:fromJsonStringWithType(check string:fromBytes(message.payload));
log:printInfo(string `Received temperature details from device: ${details.deviceId} at
${time:utcToString(details.timestamp)} with temperature: ${details.temperature}`);
}
}
Prerequisites
- • Start an MQTT broker instance, which is configured to use SSL/TLS.
Run the program by executing the following command.
$ bal run mqtt_service_ssl.baltime=2023-08-16T16:48:03.259+05:30 level=INFO module="" message="Received temperature details from device: device-id-1 at 2023-08-16T11:18:03.212400160Z with temperature: 27.5"
Tip: Run the MQTT client given in the MQTT client - SSL/TLS example to publish some messages to the topic.
Related links
PreviousPublish message
NextBasic authentication