TsgcWSPClient_MQTT | Client MQTT Connect

In order to connect to a MQTT Server, you must create first a TsgcWebSocketClient and a TsgcWSPClient_MQTT. Then you must attach MQTT Component to WebSocket Client.

 

Basic Usage

Connect to Mosquitto MQTT server using websocket protocol. Subscribe to topic: "topic1" after connect.

 


oClient = new TsgcWebSocketClient();
oClient->Host = "test.mosquitto.org";
oClient->Port = 8080;
oMQTT = new TsgcWSPClient_MQTT();
oMQTT->Client = oClient;
oClient->Active = true;
 
void OnMQTTConnect(TsgcWSConnection *Connection, const bool Session, const int ReasonCode, 
  const string ReasonName, const TsgcWSMQTTCONNACKProperties *ConnectProperties);
{
  oMQTT->Subscribe("topic1");
}

 

Client Identifier

MQTT requires a Client Identifier to identify client connection. Component sets a random value automatically but you can set your own Client Identifier if required, to do this, just handle OnBeforeConnect event and set your value on aClientIdentifier parameter.

 


void OnMQTTBeforeConnect(TsgcWSConnection *Connection, ref bool aCleanSession, 
  ref string aClientIdentifier)
{
  aClientIdentifier = "your client id";
}

Authentication

Somes servers require an user and password to authorize MQTT connections. Use Authentication property to set the value for username and password before connect to server.

 


oMQTT = new TsgcWSPClient_MQTT();
oMQTT->Authentication->Enabled = true;
oMQTT->Authentication->UserName = "your user";
oMQTT->Authentication->Password = "your password";