Messages sent by the server are received in the OnMQTTPublish event. This event has the following parameters:
Topic: is the name of the topic associated to this message.
Text: is the text of the message.
PublishProperties: if MQTT 5.0, these are the properties of the published message.
procedure OnMQTTPublish(Connection: TsgcWSConnection; aTopic, aText: string;
PublishProperties: TsgcWSMQTTPublishProperties);
begin
WriteLn('Topic: ' + aTopic + '. Message: ' + aText);
end;
The OnMQTTPublishEx event provides the published message payload in multiple formats through a TsgcWSMQTTPublishData object. This event has the following parameters:
Topic: is the name of the topic associated to this message.
Data: contains the payload of the published message. It has the following properties:
Value: the payload as a string.
Bytes: the raw payload as TBytes.
Stream: the raw payload as a TMemoryStream.
PublishProperties: if MQTT 5.0, these are the properties of the published message.
procedure OnMQTTPublishEx(Connection: TsgcWSConnection; aTopic: string;
aData: TsgcWSMQTTPublishData; PublishProperties: TsgcWSMQTTPublishProperties);
begin
// read as string
WriteLn('Topic: ' + aTopic + '. Message: ' + aData.Value);
// read as bytes
WriteLn('Bytes Length: ' + IntToStr(Length(aData.Bytes)));
// read as stream
WriteLn('Stream Size: ' + IntToStr(aData.Stream.Size));
end;