TsgcWSPClient_MQTTEvents › OnMQTTPublishEx

OnMQTTPublishEx Event

Fires on incoming PUBLISH; delivers the payload as a TsgcWSMQTTPublishData (Value, Bytes, Stream) plus the raw TsgcWSMQTTMessage.

Syntax

property OnMQTTPublishEx: TsgcWSMQTTPublishExEvent;
// TsgcWSMQTTPublishExEvent = procedure(Connection: TsgcWSConnection; aTopic: String; aData: TsgcWSMQTTPublishData; PublishProperties: TsgcWSMQTTPUBLISHProperties; aMessage: TsgcWSMQTTMessage) of object

Default Value

Remarks

Extended variant of OnMQTTPublish for binary payloads and low-level inspection. Fires for every incoming PUBLISH after the transport-level QoS handshake has finished (QoS 0 immediately; QoS 1 after PUBACK; QoS 2 after PUBREC → PUBREL → PUBCOMP). Parameters:

Handle either OnMQTTPublish or OnMQTTPublishEx (the component delivers incoming messages to whichever is assigned); OnMQTTPublishEx is preferred when the payload is not guaranteed to be valid UTF-8.

Example

procedure TForm1.MQTTPublishEx(Connection: TsgcWSConnection;
  aTopic: String; aData: TsgcWSMQTTPublishData;
  PublishProperties: TsgcWSMQTTPUBLISHProperties;
  aMessage: TsgcWSMQTTMessage);
var
  vBytes: TBytes;
begin
  Memo1.Lines.Add(Format('[%s] QoS=%d %d bytes',
    [aTopic, Ord(aMessage.QoS), Length(aData.Bytes)]));

  // read the payload as raw bytes (safe for binary content)
  vBytes := aData.Bytes;

  // or pull it through the stream API
  aData.Stream.Position := 0;
  SaveStreamToFile(aData.Stream, 'payload.bin');
end;

Back to Events