Loading...
Searching...
No Matches

Class is used for managing MQTT session and publishing position data to MQTT broker. More...

#include <com/navigine/idl/objc/NCMqttSession.h>

Inherits NSObject.

Instance Methods

(void) - connect:port:username:password:
 Method is used to connect to MQTT broker and start publishing position data.
 
(void) - setSubTopic:
 Method is used to set MQTT sub-topic for publishing position data. The final topic will be "navigine/mobile/positions/" + subTopic + "/" + deviceId.
 
(void) - addListener:
 Method is used to add MqttSessionListener class element which will notify about MQTT session connection state changes.
 
(void) - removeListener:
 Method is used for removing previously added MqttSessionListener class element.
 
(void) - disconnect
 Method is used to disconnect from MQTT broker and stop publishing position data.
 
(void) - publish:message:
 Method is used to publish a custom message to a specified MQTT topic. The message will be sent asynchronously and the result will be notified through MqttSessionListener callbacks (onMessagePublished for success, onError for failure).
 

Detailed Description

Class is used for managing MQTT session and publishing position data to MQTT broker.

Referenced from NavigineSdk.

Definition at line 19 of file NCMqttSession.h.

Method Documentation

◆ addListener:

- (void) addListener: (nullable id< NCMqttSessionListener >) listener

Method is used to add MqttSessionListener class element which will notify about MQTT session connection state changes.

Note
Do not forget to remove listener if it is no longer needed!
Parameters
listenerCorresponding MqttSessionListener class.

Swift code snippet:

// Add MQTT session listener
session.addListener(self)

Objective C code snippet:

// Add MQTT session listener
[self.mqttSession addListener:self];

◆ connect:port:username:password:

- (void) connect: (nonnull NSString *) server
port: (int32_t) port
username: (nonnull NSString *) username
password: (nonnull NSString *) password 

Method is used to connect to MQTT broker and start publishing position data.

Parameters
serverMQTT broker server hostname or IP address.
portMQTT broker server port.
usernameMQTT broker username for authentication.
passwordMQTT broker password for authentication.

Swift code snippet:

// Connect to MQTT broker
// Server: MQTT broker hostname or IP address
// Port: MQTT broker port (typically 1883 for non-SSL, 8883 for SSL)
// Username: MQTT broker username for authentication
// Password: MQTT broker password for authentication
session.connect("mqtt.example.com", port: 1883, username: "username", password: "password")

Objective C code snippet:

// Connect to MQTT broker
// Server: MQTT broker hostname or IP address
// Port: MQTT broker port (typically 1883 for non-SSL, 8883 for SSL)
// Username: MQTT broker username for authentication
// Password: MQTT broker password for authentication
[self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"username" password:@"password"];

◆ disconnect

- (void) disconnect

Method is used to disconnect from MQTT broker and stop publishing position data.

Swift code snippet:

// Disconnect from MQTT broker
session.disconnect()

Objective C code snippet:

// Disconnect from MQTT broker
[self.mqttSession disconnect];

◆ publish:message:

- (void) publish: (nonnull NSString *) topic
message: (nonnull NSString *) message 

Method is used to publish a custom message to a specified MQTT topic. The message will be sent asynchronously and the result will be notified through MqttSessionListener callbacks (onMessagePublished for success, onError for failure).

Note
The MQTT session must be connected before calling this method. Use connect method first.
Parameters
topicMQTT topic to publish the message to. Can be any valid MQTT topic string.
messageMessage content to publish. Can be any string (JSON, plain text, etc.).

Swift code snippet:

// Publish a custom message to a specific MQTT topic
// The message will be sent asynchronously and the result will be notified through listener callbacks
let customTopic = "custom/device/status"
let customMessage = "{\"status\": \"online\", \"timestamp\": \‍(Int(Date().timeIntervalSince1970 * 1000))}"
session.publish(topic: customTopic, message: customMessage)

Objective C code snippet:

// Publish a custom message to a specific MQTT topic
// The message will be sent asynchronously and the result will be notified through listener callbacks
NSString *customTopic = @"custom/device/status";
NSString *customMessage = [NSString stringWithFormat:@"{\"status\": \"online\", \"timestamp\": %lld}", (long long)([[NSDate date] timeIntervalSince1970] * 1000)];
[self.mqttSession publish:customTopic message:customMessage];

◆ removeListener:

- (void) removeListener: (nullable id< NCMqttSessionListener >) listener

Method is used for removing previously added MqttSessionListener class element.

Parameters
listenerCorresponding MqttSessionListener class to remove.

Swift code snippet:

// Remove first listener
session.removeListener(listener1)

Objective C code snippet:

// Remove first listener
[self.mqttSession removeListener:listener1];

◆ setSubTopic:

- (void) setSubTopic: (nonnull NSString *) subTopic

Method is used to set MQTT sub-topic for publishing position data. The final topic will be "navigine/mobile/positions/" + subTopic + "/" + deviceId.

Parameters
subTopicMQTT sub-topic for publishing position data. Must match pattern [0-9a-zA-Z_-]+ and cannot be empty.

Swift code snippet:

// Set MQTT sub-topic for publishing position data
// Final topic will be "navigine/mobile/positions/" + subTopic + "/" + deviceId
session.setSubTopic("location1")

Objective C code snippet:

// Set MQTT sub-topic for publishing position data
// Final topic will be "navigine/mobile/positions/" + subTopic + "/" + deviceId
[self.mqttSession setSubTopic:@"location1"];

The documentation for this class was generated from the following file: