MQTT Client Component: sgcMQ | eSeGeCe

MQTT Client

TsgcWSPClient_MQTT is a native MQTT 3.1.1 and MQTT 5.0 client. Publish and subscribe with QoS 0, 1 and 2, retained messages, a Last Will and Testament, persistent sessions, and the MQTT 5 additions: reason codes, user properties, topic aliases, shared subscriptions and enhanced authentication. The same component and the same API in Delphi and C++ Builder.

TsgcWSPClient_MQTT

Assign a TsgcTCPClient to Client, pick the protocol version with MQTTVersion, wire OnMQTTConnect and OnMQTTPublish, then call Subscribe and Publish.

Component class

TsgcWSPClient_MQTT

Specification

OASIS MQTT 3.1.1 and MQTT 5.0

Transport

TCP (1883) or TLS (8883)

Languages

Delphi, C++ Builder

Transport: plain TCP and TLS. sgcMQ connects over plain TCP and TLS. If you need MQTT over WebSocket, that requires a sgcWebSockets package, which provides the WebSocket client.

Connect, subscribe, publish

A TsgcTCPClient on the Client property puts MQTT straight on the TCP socket. Switch on TLS and point the port at 8883 for an encrypted broker.

uses
  sgcTCP_Client_WS, sgcWebSocket_Classes, sgcWebSocket_Types,
  sgcWebSocket_Protocols, sgcWebSocket_Protocol_MQTT_Client,
  sgcWebSocket_Protocol_MQTT_Message;

var
  TCPClient: TsgcTCPClient;
  MQTT: TsgcWSPClient_MQTT;
begin
  TCPClient := TsgcTCPClient.Create(nil);
  TCPClient.Host := 'broker.example.com';
  TCPClient.Port := 1883;
  TCPClient.WatchDog.Enabled := True;

  MQTT := TsgcWSPClient_MQTT.Create(nil);
  MQTT.Client := TCPClient;
  MQTT.MQTTVersion := mqtt5;
  MQTT.Authentication.Enabled  := True;
  MQTT.Authentication.UserName := 'sgc';
  MQTT.Authentication.Password := 'sgc';

  MQTT.LastWillTestament.Enabled := True;
  MQTT.LastWillTestament.Topic   := 'devices/sensor-01/status';
  MQTT.LastWillTestament.Message := 'offline';
  MQTT.LastWillTestament.QoS     := mtqsAtLeastOnce;
  MQTT.LastWillTestament.Retain  := True;

  MQTT.OnMQTTConnect := MQTTConnect;
  MQTT.OnMQTTPublish := MQTTPublish;

  TCPClient.Active := True;
end;

procedure TForm1.MQTTConnect(Connection: TsgcWSConnection;
  const Session: Boolean; const ReasonCode: Integer;
  const ReasonName: string;
  const ConnectProperties: TsgcWSMQTTCONNACKProperties);
begin
  MQTT.Subscribe('sensors/+/temperature', mtqsAtLeastOnce);
  MQTT.Publish('devices/sensor-01/status',
    'online', mtqsAtLeastOnce, True);
end;

procedure TForm1.MQTTPublish(Connection: TsgcWSConnection;
  aTopic, aText: string;
  PublishProperties: TsgcWSMQTTPublishProperties);
begin
  Memo1.Lines.Add(aTopic + ' = ' + aText);
end;
// include: sgcTCP_Client_WS.hpp, sgcWebSocket_Protocols.hpp,
// sgcWebSocket_Protocol_MQTT_Client.hpp

TsgcTCPClient *TCPClient = new TsgcTCPClient(this);
TCPClient->Host = "broker.example.com";
TCPClient->Port = 1883;
TCPClient->WatchDog->Enabled = true;

TsgcWSPClient_MQTT *MQTT = new TsgcWSPClient_MQTT(this);
MQTT->Client = TCPClient;
MQTT->MQTTVersion = mqtt5;
MQTT->Authentication->Enabled  = true;
MQTT->Authentication->UserName = "sgc";
MQTT->Authentication->Password = "sgc";

MQTT->OnMQTTConnect = MQTTConnect;
MQTT->OnMQTTPublish = MQTTPublish;

TCPClient->Active = true;

void __fastcall TForm1::MQTTPublish(TsgcWSConnection *Connection,
    UnicodeString aTopic, UnicodeString aText,
    TsgcWSMQTTPublishProperties PublishProperties)
{
  Memo1->Lines->Add(aTopic + " = " + aText);
}

// In the OnMQTTConnect handler:
MQTT->Subscribe("sensors/+/temperature", mtqsAtLeastOnce);
MQTT->Publish("devices/sensor-01/status",
  "online", mtqsAtLeastOnce, true);

Key properties & methods

The members you reach for most often.

Carrier

Client takes the TsgcTCPClient that owns the socket. Its Host, Port, TLS, TLSOptions and WatchDog decide how the bytes travel.

Protocol version

MQTTVersion selects mqtt311 or mqtt5. The MQTT 5 properties and reason codes only appear on the wire when version 5 is selected.

Authentication

Authentication.Enabled, UserName and Password fill the CONNECT packet. For MQTT 5 challenge/response schemes use Auth together with OnMQTTAuth.

Publishing

Publish(topic, text, qos, retain) sends a message, with a TStream overload for binary payloads. PublishAndWait blocks until the broker acknowledges the packet.

Subscribing

Subscribe(topic, qos) takes a single filter or a TsgcWSTopics list, and UnSubscribe mirrors it. Wildcards are + for one level and # for the remainder.

Delivery events

OnMQTTPublish delivers an inbound message. The QoS 2 handshake surfaces as OnMQTTPubRec, OnMQTTPubRel and OnMQTTPubComp, with OnMQTTPubAck for QoS 1.

Last Will

LastWillTestament carries the topic, message, QoS and retain flag the broker publishes on your behalf if the connection drops without a clean DISCONNECT.

Sessions and liveness

ConnectProperties sets MQTT 5 session expiry and limits. HeartBeat drives the keep-alive PINGREQ, with OnMQTTPing on the response.

QoS storage

The QoS property controls how outbound QoS 1 and 2 messages are stored and retried while the acknowledgement is outstanding.

Keep exploring

Online HelpFull API reference and usage guide.
All sgcMQ ComponentsBrowse the full feature matrix of all seven components.
Download Free TrialRun the client against Mosquitto, HiveMQ or your own broker.
PricingSingle, Team and Site licenses with full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and publish your first MQTT message from Delphi or C++ Builder.