TsgcWSPClient_MQTT › Methods › PublishAndWait
Publishes a message and blocks until the broker acknowledges it or the timeout elapses.
function PublishAndWait(const aTopic, aText: String; aQoS: TmqttQoS = mtqsAtMostOnce; aRetain: Boolean = False; const aPublishProperties: TsgcWSMQTTPublish_Properties = nil; const aTimeout: Integer = 10000): Boolean;
| Name | Type | Description |
|---|---|---|
aTopic | const String | Target topic name for the PUBLISH packet. |
aText | const String | UTF-8 payload of the message. |
aQoS | TmqttQoS | Delivery guarantee. Only QoS 1 and QoS 2 produce a broker acknowledgement, so QoS 0 returns True immediately after the packet is written. |
aRetain | Boolean | Retain flag forwarded to the broker. |
aPublishProperties | const TsgcWSMQTTPublish_Properties | Optional MQTT 5.0 publish properties. Pass nil when not needed. |
aTimeout | const Integer | Maximum time, in milliseconds, to wait for the matching PUBACK (QoS 1) or PUBCOMP (QoS 2). Defaults to 10000. |
True when the broker acknowledged the message before the timeout expired; False when the wait timed out or the session dropped. (Boolean)
Synchronous text overload. Useful for request/response workflows and startup scripts where the caller must know the message reached the broker before proceeding. Must not be invoked from the component's own event callbacks, since those are already serialised on the transport thread and would deadlock.
if not MQTT.PublishAndWait('alerts/critical', '{"code":500}', mtqsAtLeastOnce, False, nil, 5000) then
raise Exception.Create('broker did not acknowledge alert');
function PublishAndWait(const aTopic: String; const aStream: TStream; aQoS: TmqttQoS = mtqsAtMostOnce; aRetain: Boolean = False; const aPublishProperties: TsgcWSMQTTPublish_Properties = nil; const aTimeout: Integer = 10000): Boolean;
| Name | Type | Description |
|---|---|---|
aTopic | const String | Target topic name for the binary PUBLISH packet. |
aStream | const TStream | Stream supplying the raw bytes sent as the payload. Read in full before transmission. |
aQoS | TmqttQoS | Delivery guarantee applied to the PUBLISH packet. |
aRetain | Boolean | Retain flag forwarded to the broker. |
aPublishProperties | const TsgcWSMQTTPublish_Properties | Optional MQTT 5.0 properties, commonly used to set Content Type for binary payloads. |
aTimeout | const Integer | Maximum milliseconds to wait for the QoS 1/2 acknowledgement. Defaults to 10000. |
True on acknowledgement, False on timeout or disconnection. (Boolean)
Synchronous binary overload suited to file upload flows where the calling code must confirm each chunk was stored by the broker. Avoid invoking it from event handlers, and use realistic timeouts when sending large payloads over slow networks.
oStream := TMemoryStream.Create;
try
oStream.LoadFromFile('config.bin');
if not MQTT.PublishAndWait('devices/123/config', oStream, mtqsExactlyOnce, True, nil, 30000) then
ShowMessage('config upload failed');
finally
oStream.Free;
end;