TsgcWSPClient_MQTTMethods › PublishAndWait

PublishAndWait Method

Publishes a message and blocks until the broker acknowledges it or the timeout elapses.

Overloads

Overload 1

Syntax

function PublishAndWait(const aTopic, aText: String; aQoS: TmqttQoS = mtqsAtMostOnce; aRetain: Boolean = False; const aPublishProperties: TsgcWSMQTTPublish_Properties = nil; const aTimeout: Integer = 10000): Boolean;

Parameters

NameTypeDescription
aTopicconst StringTarget topic name for the PUBLISH packet.
aTextconst StringUTF-8 payload of the message.
aQoSTmqttQoSDelivery guarantee. Only QoS 1 and QoS 2 produce a broker acknowledgement, so QoS 0 returns True immediately after the packet is written.
aRetainBooleanRetain flag forwarded to the broker.
aPublishPropertiesconst TsgcWSMQTTPublish_PropertiesOptional MQTT 5.0 publish properties. Pass nil when not needed.
aTimeoutconst IntegerMaximum time, in milliseconds, to wait for the matching PUBACK (QoS 1) or PUBCOMP (QoS 2). Defaults to 10000.

Return Value

True when the broker acknowledged the message before the timeout expired; False when the wait timed out or the session dropped. (Boolean)

Remarks

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.

Example

if not MQTT.PublishAndWait('alerts/critical', '{"code":500}', mtqsAtLeastOnce, False, nil, 5000) then
  raise Exception.Create('broker did not acknowledge alert');

Overload 2

Syntax

function PublishAndWait(const aTopic: String; const aStream: TStream; aQoS: TmqttQoS = mtqsAtMostOnce; aRetain: Boolean = False; const aPublishProperties: TsgcWSMQTTPublish_Properties = nil; const aTimeout: Integer = 10000): Boolean;

Parameters

NameTypeDescription
aTopicconst StringTarget topic name for the binary PUBLISH packet.
aStreamconst TStreamStream supplying the raw bytes sent as the payload. Read in full before transmission.
aQoSTmqttQoSDelivery guarantee applied to the PUBLISH packet.
aRetainBooleanRetain flag forwarded to the broker.
aPublishPropertiesconst TsgcWSMQTTPublish_PropertiesOptional MQTT 5.0 properties, commonly used to set Content Type for binary payloads.
aTimeoutconst IntegerMaximum milliseconds to wait for the QoS 1/2 acknowledgement. Defaults to 10000.

Return Value

True on acknowledgement, False on timeout or disconnection. (Boolean)

Remarks

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.

Example

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;

Back to Methods