MQTT Protocol

Lightweight publish-subscribe messaging protocol designed for IoT and constrained devices. Full support for MQTT 3.1.1 and MQTT 5.0 specifications.

MQTT subprotocol client

Lightweight publish-subscribe messaging for IoT and constrained devices. Full MQTT 3.1.1 and MQTT 5.0 support, over WebSocket or raw TCP.

Component class

TsgcWSPClient_MQTT

Protocol

MQTT 3.1.1 & MQTT 5.0

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Standard / Professional / Enterprise

Drop the component, set a few properties, go

Pair a TsgcWebSocketClient with TsgcWSPClient_MQTT, wire OnMQTTConnect / OnMQTTPublish, then Subscribe and Publish.

uses
  sgcWebSocket, sgcWebSocket_Protocol_MQTT_Client,
  sgcWebSocket_Protocol_MQTT_Message;

var
  WSClient: TsgcWebSocketClient;
  MQTT: TsgcWSPClient_MQTT;
begin
  WSClient := TsgcWebSocketClient.Create(nil);
  WSClient.Host := 'www.esegece.com';
  WSClient.Port := 15675;

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

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

  WSClient.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/#');
end;

procedure TForm1.MQTTPublish(Connection: TsgcWSConnection;
  aTopic, aText: string;
  PublishProperties: TsgcWSMQTTPublishProperties);
begin
  Memo1.Lines.Add(aTopic + ': ' + aText);
end;

// Publish a retained QoS 1 message
MQTT.Publish('sensors/temperature/room1',
  '{"value":22.5,"unit":"C"}', mtqsAtLeastOnce, True);
// uses: sgcWebSocket, sgcWebSocket_Protocol_MQTT_Client
TsgcWebSocketClient *WSClient = new TsgcWebSocketClient(this);
WSClient->Host = "www.esegece.com";
WSClient->Port = 15675;

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

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

WSClient->Active = true;

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

MQTT->Publish("sensors/temperature/room1",
  "{\"value\":22.5,\"unit\":\"C\"}", mtqsAtLeastOnce, true);
using esegece.sgcWebSockets;

var WSClient = new TsgcWebSocketClient();
WSClient.Options.Parameters = "/ws";

var MQTT = new TsgcWSPClient_MQTT();
MQTT.Client = WSClient;
MQTT.Authentication.Enabled  = true;
MQTT.Authentication.UserName = "sgc";
MQTT.Authentication.Password = "sgc";
MQTT.HeartBeat.Enabled  = true;
MQTT.HeartBeat.Interval = 5;

MQTT.OnMQTTConnect += (conn, session, code, name, props) => MQTT.Subscribe("sensors/temperature/#");
MQTT.OnMQTTPublish += (conn, topic, text, props) => Console.WriteLine(topic + ": " + text);

WSClient.Host = "www.esegece.com";
WSClient.Port = 15675;
WSClient.Active = true;

MQTT.Publish("sensors/temperature/room1",
  "{\"value\":22.5,\"unit\":\"C\"}", TmqttQoS.mtqsAtLeastOnce, true);

What's inside

10 published properties, 9 methods and 13 events — matched against the TsgcWSPClient_MQTT reference.

Protocol versions

MQTTVersion selects the level advertised in the CONNECT packet — MQTT 3.1.1 or MQTT 5.0. ConnectProperties carries the MQTT 5 session-expiry, receive-maximum, packet-size, topic-alias and extended-auth fields.

WebSocket or raw TCP

Plug the Client property into a TsgcWebSocketClient to run MQTT over WebSockets, or into a TsgcWSMQTTBroker via Broker to use the native TCP transport on port 1883/8883.

QoS & retain

Publish at QoS 0, 1 or 2 with optional retain. QoS property controls default level, retry interval and timeout. PublishAndWait blocks until the broker acknowledges — useful for transactional publishes.

Last Will & Testament

LastWillTestament registers the topic, payload, QoS and retain flag the broker will publish on this client's behalf if the connection drops ungracefully — a clean way to signal device disconnects.

Subscriptions & events

Subscribe / UnSubscribe accept topic filters with + and # wildcards. OnMQTTPublish delivers incoming messages; OnMQTTPublishEx gives raw bytes / stream. Full QoS 2 handshake exposed via OnMQTTPubRec, OnMQTTPubRel, OnMQTTPubComp.

Keep-alive & auth

HeartBeat sends PINGREQ on a timer and detects silent broker drops. Authentication sends a UserName/Password in CONNECT. MQTT 5 enhanced auth: call Auth and handle OnMQTTAuth for SCRAM-style challenges.

Specifications & references

Authoritative sources for the protocols this component implements.

Documentation & Demos

Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.

Online Help — Protocol_MQTT Full property, method and event reference for this component.
Demo Project — Demos\Protocols\MQTT Ready-to-run example project. Ships inside the sgcWebSockets package — download the trial below.
Technical Document (PDF) Features, quick start, code samples for Delphi, C++ Builder and .NET and primary-source references — this component only.
User Manual (PDF) Comprehensive manual covering every component in the library.

Ready to Get Started with MQTT?

Download the free trial and start building IoT messaging solutions in minutes.