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.

Classe do componente

TsgcWSPClient_MQTT

Protocolo

MQTT 3.1.1 & MQTT 5.0

Plataformas

Windows, macOS, Linux, iOS, Android

Edição

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);
// usa: 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);

O que tem por dentro

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.

Especificações & referências

Authoritative sources for the protocols this component implements.

Documentação & Demos

Acesse a referência do componente, pegue o projeto demo pronto para executar e baixe a versão de avaliação.

Ajuda online — Protocol_MQTT Referência completa de propriedades, métodos e eventos deste componente.
Projeto demo — Demos\Protocols\MQTT Exemplo pronto para executar. Acompanha o pacote sgcWebSockets — baixe a versão de avaliação abaixo.
Documento técnico (PDF) Features, quick start, code samples for Delphi, C++ Builder and .NET and primary-source references — this component only.
Manual do usuário (PDF) Manual completo cobrindo todos os componentes da biblioteca.

Ready to Get Started with MQTT?

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