The Messaging API is used to initialize, connect and communicate with the ClearBlade MQTT Broker for publishing messages, subscribing, unsubscribing to and from topics and disconnect.
Please make sure that you have initialized and authenticated with the ClearBlade platform prior to using the Messaging API. This is important because the ClearBlade MQTT Broker requires the authentication token to establish a successful connection. This authentication token can only be obtained by initializing and authenticaing with the ClearBlade platform
You will need to clone the following C# repository for using the Messaging API:
Initialize and Connect
The first step is to create a new
ClearBladeMessageService object by passing the client ID and messaging QoS (optional). The
ClearBladeMessageService constructor will then initialize and connect with the MQTT Broker.
string clientID = “ClearBladeJavaTest”; ClearBladeMessageService message = new ClearBladeMessageService(clientID); // QoS = 0 Default
int qos = 1; // QoS can be 0,1 or 2 string clientID = “ClearBladeC#Test”; ClearBladeMessageService message = new ClearBladeMessageService(clientID, qos);
int qos = 1; // QoS can be 0,1 or 2 string clientID = “ClearBladeC#Test”; bool cleanSession = true //or false ClearBladeMessageService message = new ClearBladeMessageService(clientID, qos);
After the connection is successful, you can publish, subscribe, unsubscribe or disconnect using the
The publish function takes a topic and message of type
string and publishes to the MQTT Broker.
string topic = "yourTopic"; string message = "yourMessage"; message.publish(topic, message);
string topic = "yourTopic"; string message = "yourMessage"; int qos = ""; bool retain = "true" //or false message.publish(topic, message, qos, retain);
The subscribe function takes a topic of type
string and a callback to handle the arrived messages.
string topic = "topicToSubscribe"; message.subscribe(topic);
The unsubscribe function takes a topic of type `
string topic = "topicToUnsubscribe"; message.unsubscribe(topic);
The disconnect function is used to disconnect from the MQTT Broker. Note that this does not disconnect the user from the ClearBlade platform. User logout needs to be called separately.