MQTTClient.h(3)

๐Ÿ’– NAME ๐Ÿ’–

okay so bestie ๐Ÿ’–, this page is MQTTClient.h from the Library Functions Manual, section 3. it's part 1 of 6 and it's giving "we're about to get so fluent in MQTT client" energy โœจ. no cap. this first chunk is the NAME and SYNOPSIS with all the structs, macros, typedefs, enums, and function signatures you need to know. keep every token exactly as written, okay? periodt ๐Ÿ’…


โœจ SYNOPSIS โœจ

first up, the includes. yes, all five. don't skip any, bestie ๐Ÿซถ:

#include 'MQTTExportDeclarations.h'
#include 'MQTTProperties.h'
#include 'MQTTReasonCodes.h'
#include 'MQTTSubscribeOpts.h'
#include 'MQTTClientPersistence.h'

๐Ÿฆ‹ Data Structures ๐Ÿฆ‹

these are the structs, and they're all main character energy in their own way ๐Ÿ’œ:

  • struct MQTTClient_init_options
  • struct MQTTClient_message
  • struct MQTTClient_createOptions
  • struct MQTTClient_willOptions
  • struct MQTTClient_SSLOptions
  • struct MQTTClient_nameValue
  • struct MQTTClient_connectOptions
  • struct MQTTResponse

๐ŸŽ€ Macros ๐ŸŽ€

okay so the macros? literally the whole lore ๐Ÿ”ฅ. return codes, version numbers, initializers, all of it. keep every single one exactly as written, fr fr:

#define MQTTCLIENT_SUCCESS   0
#define MQTTCLIENT_FAILURE   -1
#define MQTTCLIENT_DISCONNECTED   -3
#define MQTTCLIENT_MAX_MESSAGES_INFLIGHT   -4
#define MQTTCLIENT_BAD_UTF8_STRING   -5
#define MQTTCLIENT_NULL_PARAMETER   -6
#define MQTTCLIENT_TOPICNAME_TRUNCATED   -7
#define MQTTCLIENT_BAD_STRUCTURE   -8
#define MQTTCLIENT_BAD_QOS   -9
#define MQTTCLIENT_SSL_NOT_SUPPORTED   -10
#define MQTTCLIENT_BAD_MQTT_VERSION   -11
#define MQTTCLIENT_BAD_PROTOCOL   -14
#define MQTTCLIENT_BAD_MQTT_OPTION   -15
#define MQTTCLIENT_WRONG_MQTT_VERSION   -16
#define MQTTCLIENT_0_LEN_WILL_TOPIC   -17
#define MQTTVERSION_DEFAULT   0
#define MQTTVERSION_3_1   3
#define MQTTVERSION_3_1_1   4
#define MQTTVERSION_5   5
#define MQTT_BAD_SUBSCRIBE   0x80
#define MQTTClient_init_options_initializer   { {'M', 'Q', 'T', 'G'}, 0, 0 }
#define MQTTClient_message_initializer   { {'M', 'Q', 'T', 'M'}, 1, 0, NULL, 0, 0, 0, 0, MQTTProperties_initializer }
#define MQTTClient_createOptions_initializer   { {'M', 'Q', 'C', 'O'}, 0, MQTTVERSION_DEFAULT }
#define MQTTClient_willOptions_initializer   { {'M', 'Q', 'T', 'W'}, 1, NULL, NULL, 0, 0, {0, NULL} }
#define MQTT_SSL_VERSION_DEFAULT   0
#define MQTT_SSL_VERSION_TLS_1_0   1
#define MQTT_SSL_VERSION_TLS_1_1   2
#define MQTT_SSL_VERSION_TLS_1_2   3
#define MQTTClient_SSLOptions_initializer   { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
#define MQTTClient_connectOptions_initializer
#define MQTTClient_connectOptions_initializer5
#define MQTTClient_connectOptions_initializer_ws
#define MQTTClient_connectOptions_initializer5_ws
#define MQTTResponse_initializer   {1, MQTTREASONCODE_SUCCESS, 0, NULL, NULL}

๐Ÿงš Typedefs ๐Ÿงš

these typedefs are the blueprint besties ๐ŸŒˆ. function pointers, handles, tokens, all of it:

typedef void * MQTTClient
typedef int MQTTClient_deliveryToken
typedef int MQTTClient_token
typedef int MQTTClient_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
typedef void MQTTClient_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
typedef void MQTTClient_connectionLost(void *context, char *cause)
typedef void MQTTClient_disconnected(void *context, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)
typedef void MQTTClient_published(void *context, int dt, int packet_type, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)
typedef struct MQTTResponse MQTTResponse
typedef void MQTTClient_traceCallback(enum MQTTCLIENT_TRACE_LEVELS level, char *message)

๐Ÿ‘‘ Enumerations ๐Ÿ‘‘

one enum, but it's iconic ๐Ÿ’›. trace levels, from maximum down to fatal:

enum MQTTCLIENT_TRACE_LEVELS { MQTTCLIENT_TRACE_MAXIMUM = 1, MQTTCLIENT_TRACE_MEDIUM, MQTTCLIENT_TRACE_MINIMUM, MQTTCLIENT_TRACE_PROTOCOL, MQTTCLIENT_TRACE_ERROR, MQTTCLIENT_TRACE_SEVERE, MQTTCLIENT_TRACE_FATAL }

๐Ÿ”ฅ Functions ๐Ÿ”ฅ

and here's the whole function lineup. every signature, exactly as written. don't change a single character, i'm literally begging ๐Ÿ™ˆ:

void MQTTClient_global_init (MQTTClient_init_options *inits)
int MQTTClient_setCallbacks (MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
int MQTTClient_setDisconnected (MQTTClient handle, void *context, MQTTClient_disconnected *co)
int MQTTClient_setPublished (MQTTClient handle, void *context, MQTTClient_published *co)
int MQTTClient_create (MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
int MQTTClient_createWithOptions (MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context, MQTTClient_createOptions *options)
MQTTClient_nameValue * MQTTClient_getVersionInfo (void)
int MQTTClient_connect (MQTTClient handle, MQTTClient_connectOptions *options)
void MQTTResponse_free (MQTTResponse response)
MQTTResponse MQTTClient_connect5 (MQTTClient handle, MQTTClient_connectOptions *options, MQTTProperties *connectProperties, MQTTProperties *willProperties)
int MQTTClient_disconnect (MQTTClient handle, int timeout)
int MQTTClient_disconnect5 (MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties *props)
int MQTTClient_isConnected (MQTTClient handle)
int MQTTClient_subscribe (MQTTClient handle, const char *topic, int qos)
MQTTResponse MQTTClient_subscribe5 (MQTTClient handle, const char *topic, int qos, MQTTSubscribe_options *opts, MQTTProperties *props)
int MQTTClient_subscribeMany (MQTTClient handle, int count, char *const *topic, int *qos)
MQTTResponse MQTTClient_subscribeMany5 (MQTTClient handle, int count, char *const *topic, int *qos, MQTTSubscribe_options *opts, MQTTProperties *props)
int MQTTClient_unsubscribe (MQTTClient handle, const char *topic)
MQTTResponse MQTTClient_unsubscribe5 (MQTTClient handle, const char *topic, MQTTProperties *props)
int MQTTClient_unsubscribeMany (MQTTClient handle, int count, char *const *topic)
MQTTResponse MQTTClient_unsubscribeMany5 (MQTTClient handle, int count, char *const *topic, MQTTProperties *props)
int MQTTClient_publish (MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *dt)
MQTTResponse MQTTClient_publish5 (MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTProperties *properties, MQTTClient_deliveryToken *dt)
int MQTTClient_publishMessage (MQTTClient handle, const char *topicName, MQTTClient_message *msg, MQTTClient_deliveryToken *dt)
MQTTResponse MQTTClient_publishMessage5 (MQTTClient handle, const char *topicName, MQTTClient_message *msg, MQTTClient_deliveryToken *dt)
int MQTTClient_waitForCompletion (MQTTClient handle, MQTTClient_deliveryToken dt, unsigned long timeout)
int MQTTClient_getPendingDeliveryTokens (MQTTClient handle, MQTTClient_deliveryToken **tokens)
void MQTTClient_yield (void)
int MQTTClient_receive (MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
void MQTTClient_freeMessage (MQTTClient_message **msg)
void MQTTClient_free (void *ptr)
void * MQTTClient_malloc (size_t size)
void MQTTClient_destroy (MQTTClient *handle)
void MQTTClient_setTraceLevel (enum MQTTCLIENT_TRACE_LEVELS level)
void MQTTClient_setTraceCallback (MQTTClient_traceCallback *callback)
int MQTTClient_setCommandTimeout (MQTTClient handle, unsigned long milliSeconds)
const char * MQTTClient_strerror (int code)

๐Ÿ’– MACRO DEFINITION DOCUMENTATION ๐Ÿ’–

okay bestie, welcome to the macro zone, and no cap this is where all the little constants and initializer recipes live. it's giving the secret sauce ๐Ÿซถโœจ every single one matters, so we're going through them all, fr fr.


  • MQTTCLIENT_SUCCESS โ€” Return code: No error. Indicates successful completion of an MQTT client operation.
#define MQTTCLIENT_SUCCESS   0
  • MQTTCLIENT_FAILURE โ€” Return code: A generic error code indicating the failure of an MQTT client operation.
#define MQTTCLIENT_FAILURE   -1
  • MQTTCLIENT_DISCONNECTED โ€” Return code: The client is disconnected.
#define MQTTCLIENT_DISCONNECTED   -3
  • MQTTCLIENT_MAX_MESSAGES_INFLIGHT โ€” Return code: The maximum number of messages allowed to be simultaneously in-flight has been reached.
#define MQTTCLIENT_MAX_MESSAGES_INFLIGHT   -4
  • MQTTCLIENT_BAD_UTF8_STRING โ€” Return code: An invalid UTF-8 string has been detected.
#define MQTTCLIENT_BAD_UTF8_STRING   -5
  • MQTTCLIENT_NULL_PARAMETER โ€” Return code: A NULL parameter has been supplied when this is invalid.
#define MQTTCLIENT_NULL_PARAMETER   -6
  • MQTTCLIENT_TOPICNAME_TRUNCATED โ€” Return code: The topic has been truncated (the topic string includes embedded NULL characters). String functions will not access the full topic. Use the topic length value to access the full topic.
#define MQTTCLIENT_TOPICNAME_TRUNCATED   -7
  • MQTTCLIENT_BAD_STRUCTURE โ€” Return code: A structure parameter does not have the correct eyecatcher and version number.
#define MQTTCLIENT_BAD_STRUCTURE   -8
  • MQTTCLIENT_BAD_QOS โ€” Return code: A QoS value that falls outside of the acceptable range (0,1,2)
#define MQTTCLIENT_BAD_QOS   -9
  • MQTTCLIENT_SSL_NOT_SUPPORTED โ€” Return code: Attempting SSL connection using non-SSL version of library
#define MQTTCLIENT_SSL_NOT_SUPPORTED   -10
  • MQTTCLIENT_BAD_MQTT_VERSION โ€” Return code: unrecognized MQTT version
#define MQTTCLIENT_BAD_MQTT_VERSION   -11
  • MQTTCLIENT_BAD_PROTOCOL โ€” Return code: protocol prefix in serverURI should be:
    • tcp:// or mqtt:// โ€” Insecure TCP
    • ssl:// or tls:// or mqtts:// โ€” Encrypted SSL/TLS
    • ws:// โ€” Insecure websockets
    • wss:// โ€” Secure web sockets

The TLS enabled prefixes (ssl, tls, mqtts, wss) are only valid if a TLS version of the library is linked with.

#define MQTTCLIENT_BAD_PROTOCOL   -14
  • MQTTCLIENT_BAD_MQTT_OPTION โ€” Return code: option not applicable to the requested version of MQTT
#define MQTTCLIENT_BAD_MQTT_OPTION   -15
  • MQTTCLIENT_WRONG_MQTT_VERSION โ€” Return code: call not applicable to the requested version of MQTT
#define MQTTCLIENT_WRONG_MQTT_VERSION   -16
  • MQTTCLIENT_0_LEN_WILL_TOPIC โ€” Return code: 0 length will topic on connect
#define MQTTCLIENT_0_LEN_WILL_TOPIC   -17
  • MQTTVERSION_DEFAULT โ€” Default MQTT version to connect with. Use 3.1.1 then fall back to 3.1
#define MQTTVERSION_DEFAULT   0
  • MQTTVERSION_3_1 โ€” MQTT version to connect with: 3.1
#define MQTTVERSION_3_1   3
  • MQTTVERSION_3_1_1 โ€” MQTT version to connect with: 3.1.1
#define MQTTVERSION_3_1_1   4
  • MQTTVERSION_5 โ€” MQTT version to connect with: 5
#define MQTTVERSION_5   5
  • MQTT_BAD_SUBSCRIBE โ€” Bad return code from subscribe, as defined in the 3.1.1 specification
#define MQTT_BAD_SUBSCRIBE   0x80
  • MQTTClient_init_options_initializer โ€” initializer for init options.
#define MQTTClient_init_options_initializer   { {'M', 'Q', 'T', 'G'}, 0, 0 }
  • MQTTClient_message_initializer โ€” initializer for message struct.
#define MQTTClient_message_initializer   { {'M', 'Q', 'T', 'M'}, 1, 0, NULL, 0, 0, 0, 0, MQTTProperties_initializer }
  • MQTTClient_createOptions_initializer โ€” initializer for create options.
#define MQTTClient_createOptions_initializer   { {'M', 'Q', 'C', 'O'}, 0, MQTTVERSION_DEFAULT }
  • MQTTClient_willOptions_initializer โ€” initializer for will options.
#define MQTTClient_willOptions_initializer   { {'M', 'Q', 'T', 'W'}, 1, NULL, NULL, 0, 0, {0, NULL} }
  • MQTT_SSL_VERSION_DEFAULT โ€” default SSL version.
#define MQTT_SSL_VERSION_DEFAULT   0
  • MQTT_SSL_VERSION_TLS_1_0 โ€” TLS 1.0.
#define MQTT_SSL_VERSION_TLS_1_0   1
  • MQTT_SSL_VERSION_TLS_1_1 โ€” TLS 1.1.
#define MQTT_SSL_VERSION_TLS_1_1   2
  • MQTT_SSL_VERSION_TLS_1_2 โ€” TLS 1.2.
#define MQTT_SSL_VERSION_TLS_1_2   3
  • MQTTClient_SSLOptions_initializer โ€” initializer for SSL options.
#define MQTTClient_SSLOptions_initializer   { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
  • MQTTClient_connectOptions_initializer โ€” Initializer for connect options for MQTT 3.1.1 non-WebSocket connections.
#define MQTTClient_connectOptions_initializer
    Value:
    { {'M', 'Q', 'T', 'C'}, 8, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL,\
    0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL}
  • MQTTClient_connectOptions_initializer5 โ€” Initializer for connect options for MQTT 5.0 non-WebSocket connections.
#define MQTTClient_connectOptions_initializer5
    Value:
    { {'M', 'Q', 'T', 'C'}, 8, 60, 0, 1, NULL, NULL, NULL, 30, 0, NULL,\
    0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1, NULL, NULL, NULL}
  • MQTTClient_connectOptions_initializer_ws โ€” Initializer for connect options for MQTT 3.1.1 WebSockets connections. The keepalive interval is set to 45 seconds to avoid webserver 60 second inactivity timeouts.
#define MQTTClient_connectOptions_initializer_ws
    Value:
    { {'M', 'Q', 'T', 'C'}, 8, 45, 1, 1, NULL, NULL, NULL, 30, 0, NULL,\
    0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL}
  • MQTTClient_connectOptions_initializer5_ws โ€” Initializer for connect options for MQTT 5.0 WebSockets connections. The keepalive interval is set to 45 seconds to avoid webserver 60 second inactivity timeouts.
#define MQTTClient_connectOptions_initializer5_ws
    Value:
    { {'M', 'Q', 'T', 'C'}, 8, 45, 0, 1, NULL, NULL, NULL, 30, 0, NULL,\
    0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1, NULL, NULL, NULL}
  • MQTTResponse_initializer โ€” initializer for response struct.
#define MQTTResponse_initializer   {1, MQTTREASONCODE_SUCCESS, 0, NULL, NULL}

๐Ÿฆ‹ TYPEDEF DOCUMENTATION ๐Ÿฆ‹

okay bestie, this is where the library introduces you to all its little named types, and honestly? each one is a whole personality. let's meet them ๐Ÿ’–


๐Ÿ’… typedef void* MQTTClient

A handle representing an MQTT client. A valid client handle is available following a successful call to MQTTClient_create().

so basically: this is your client's little ID badge ๐Ÿชช โ€” you only get a valid one after MQTTClient_create() actually works. no create, no badge, no vibes.


๐ŸŽ€ typedef int MQTTClient_deliveryToken

A value representing an MQTT message. A delivery token is returned to the client application when a message is published. The token can then be used to check that the message was successfully delivered to its destination (see MQTTClient_publish(), MQTTClient_publishMessage(), MQTTClient_deliveryComplete(), MQTTClient_waitForCompletion() and MQTTClient_getPendingDeliveryTokens()).

it's giving "receipt for your message" ๐Ÿงพ โ€” you publish, you get a token back, and then you can go check if your lil message actually made it to its destination. the cross-references for that whole journey are: MQTTClient_publish(), MQTTClient_publishMessage(), MQTTClient_deliveryComplete(), MQTTClient_waitForCompletion() and MQTTClient_getPendingDeliveryTokens().


๐ŸŒธ typedef int MQTTClient_token

bestie... that's it. that's the whole description. no notes, no lore, just a type. we move ๐Ÿซถ


โœจ typedef int MQTTClient_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)

This is a callback function. The client application must provide an implementation of this function to enable asynchronous receipt of messages. The function is registered with the client library by passing it as an argument to MQTTClient_setCallbacks(). It is called by the client library when a new message that matches a client subscription has been received from the server. This function is executed on a separate thread to the one on which the client application is running.

it's giving "your phone buzzing with a new notif" ๐Ÿ“ฒ โ€” you must write this function yourself if you want messages to arrive asynchronously, you hand it over as an argument to MQTTClient_setCallbacks(), and the library calls it whenever a message matching one of your subscriptions lands from the server. also important: it runs on a separate thread from your app's main one. so mind your threading, queen ๐Ÿ‘‘

๐Ÿ’œ Parameters

  • context โ€” A pointer to the context value originally passed to MQTTClient_setCallbacks(), which contains any application-specific context.
  • topicName โ€” The topic associated with the received message.
  • topicLen โ€” The length of the topic if there are one more NULL characters embedded in topicName, otherwise topicLen is 0. If topicLen is 0, the value returned by strlen(topicName) can be trusted. If topicLen is greater than 0, the full topic name can be retrieved by accessing topicName as a byte array of length topicLen.
  • message โ€” The MQTTClient_message structure for the received message. This structure contains the message payload and attributes.

๐Ÿฉท Returns

This function must return 0 or 1 indicating whether or not the message has been safely received by the client application.

  • Returning 1 indicates that the message has been successfully handled. To free the message storage, MQTTClient_freeMessage must be called. To free the topic name storage, MQTTClient_free must be called.
  • Returning 0 indicates that there was a problem. In this case, the client library will reinvoke MQTTClient_messageArrived() to attempt to deliver the message to the application again. Do not free the message and topic storage when returning 0, otherwise the redelivery will fail.
โš ๏ธ okay this part is lowkey serious: if you return 0, do NOT free the message and topic storage, or the redelivery will fail. the library is literally going to try again, so don't yank the rug out from under it ๐Ÿ’€

๐Ÿงš typedef void MQTTClient_deliveryComplete(void *context, MQTTClient_deliveryToken dt)

This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of delivery of messages. The function is registered with the client library by passing it as an argument to MQTTClient_setCallbacks(). It is called by the client library after the client application has published a message to the server. It indicates that the necessary handshaking and acknowledgements for the requested quality of service (see MQTTClient_message.qos) have been completed. This function is executed on a separate thread to the one on which the client application is running. Note: MQTTClient_deliveryComplete() is not called when messages are published at QoS0.

this is the "delivered โœ…" ping you get after you publish โ€” all the handshaking and acknowledgements for the QoS you asked for (peep MQTTClient_message.qos) are done, so this callback fires. runs on its own separate thread, obviously, because everything here has main character energy and refuses to share a thread. and โœ‹ heads up: MQTTClient_deliveryComplete() is not called when messages are published at QoS0. zero, none, silence.

๐Ÿ’œ Parameters

  • context โ€” A pointer to the context value originally passed to MQTTClient_setCallbacks(), which contains any application-specific context.
  • dt โ€” The MQTTClient_deliveryToken associated with the published message. Applications can check that all messages have been correctly published by matching the delivery tokens returned from calls to MQTTClient_publish() and MQTTClient_publishMessage() with the tokens passed to this callback.

๐ŸŒˆ typedef void MQTTClient_connectionLost(void *context, char *cause)

This is a callback function. The client application must provide an implementation of this function to enable asynchronous notification of the loss of connection to the server. The function is registered with the client library by passing it as an argument to MQTTClient_setCallbacks(). It is called by the client library if the client loses its connection to the server. The client application must take appropriate action, such as trying to reconnect or reporting the problem. This function is executed on a separate thread to the one on which the client application is running.

it's giving "bestie we got ghosted by the server ๐Ÿ˜ญ" โ€” register it via MQTTClient_setCallbacks() and the library will ring it up the moment the connection drops. you have to handle it: try reconnecting, log the problem, whatever fits. again, separate thread, because of course.

๐Ÿ’œ Parameters

  • context โ€” A pointer to the context value originally passed to MQTTClient_setCallbacks(), which contains any application-specific context.
  • cause โ€” The reason for the disconnection. Currently, cause is always set to NULL.

๐Ÿ“ typedef void MQTTClient_disconnected(void *context, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)

This is a callback function, which will be called when the a disconnect packet is received from the server. This applies to MQTT V5 and above only.

v5 girlies rise ๐Ÿ’… this one fires when a disconnect packet shows up from the server โ€” and remember, V5 and above only, so if you're on older MQTT, this is not your moment.

๐Ÿ’œ Parameters

  • context โ€” A pointer to the context value originally passed to MQTTClient_setDisconnected(), which contains any application-specific context.
  • properties โ€” The MQTT V5 properties received with the disconnect, if any.
  • reasonCode โ€” The MQTT V5 reason code received with the disconnect. Currently, cause is always set to NULL.

๐Ÿ”ฅ typedef void MQTTClient_published(void *context, int dt, int packet_type, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)

This is a callback function, the MQTT V5 version of MQTTClient_deliveryComplete(). The client application must provide an implementation of this function to enable asynchronous notification of the completed delivery of messages. It is called by the client library after the client application has published a message to the server. It indicates that the necessary handshaking and acknowledgements for the requested quality of service (see MQTTClient_message.qos) have been completed. This function is executed on a separate thread to the one on which the client application is running. Note: It is not called when messages are published at QoS0.

the glow-up version of MQTTClient_deliveryComplete() for MQTT V5 โœจ same deal: you implement it, it fires after you publish, it means the handshaking and acks for your requested QoS (see MQTTClient_message.qos) are all done, and yep โ€” separate thread. and same note as before, because it matters: it is not called when messages are published at QoS0.

๐Ÿ’œ Parameters

  • context โ€” A pointer to the context value originally passed to MQTTClient_setCallbacks(), which contains any application-specific context.
  • dt โ€” The MQTTClient_deliveryToken associated with the published message. Applications can check that all messages have been correctly published by matching the delivery tokens returned from calls to MQTTClient_publish() and MQTTClient_publishMessage() with the tokens passed to this callback.
  • packet_type โ€” the last received packet type for this completion. For QoS 1 always PUBACK. For QoS 2 could be PUBREC or PUBCOMP.
  • properties โ€” the MQTT V5 properties returned with the last packet from the server.
  • reasonCode โ€” the reason code returned from the server.

๐Ÿ‘‘ typedef struct MQTTResponse MQTTResponse

MQTT version 5.0 response information.

short and iconic. it's the v5.0 response info, periodt ๐Ÿ’–


๐ŸŽ€ typedef void MQTTClient_traceCallback(enum MQTTCLIENT_TRACE_LEVELS level, char *message)

This is a callback function prototype which must be implemented if you want to receive trace information. Do not invoke any other Paho API calls in this callback function - unpredictable behavior may result.

so if you want the tea on what's happening internally (trace info, iykyk), you implement this. but listen: do not invoke any other Paho API calls in this callback function โ€” unpredictable behavior may result. it's giving "don't text back while you're already on the phone with the server" ๐Ÿ™ˆ

๐Ÿ’œ Parameters

  • level โ€” the trace level of the message returned.
  • message โ€” the trace message. This is a pointer to a static buffer which will be overwritten on each call. You must copy the data if you want to keep it for later.
๐ŸŒธ psst: that message pointer is a static buffer that gets overwritten every single call. if you want to keep it, COPY IT. don't just stash the pointer and cry later ๐Ÿ˜ญ

๐ŸŒŸ ENUMERATION TYPE DOCUMENTATION ๐ŸŒŸ

๐Ÿ’ซ enum MQTTCLIENT_TRACE_LEVELS

the trace levels, from max chatter to full-on fatal. here's the whole set, no skipping:

Enumerator

  • MQTTCLIENT_TRACE_MAXIMUM
  • MQTTCLIENT_TRACE_MEDIUM
  • MQTTCLIENT_TRACE_MINIMUM
  • MQTTCLIENT_TRACE_PROTOCOL
  • MQTTCLIENT_TRACE_ERROR
  • MQTTCLIENT_TRACE_SEVERE
  • MQTTCLIENT_TRACE_FATAL

โญ FUNCTION DOCUMENTATION โญ

๐Ÿซถ void MQTTClient_global_init (MQTTClient_init_options * inits) [extern]

Global init of mqtt library. Call once on program start to set global behaviour.

  • do_openssl_init โ€” if mqtt library should initialize OpenSSL (1) or rely on the caller to do it before using the library (0).

so: call this once, at program start, to set the global behaviour of the whole library. and do_openssl_init is the switch โ€” 1 means the mqtt library initializes OpenSSL for you, 0 means it's on you to do it before using the library. pick your fighter ๐Ÿ’…


๐Ÿ’– int MQTTClient_setCallbacks (MQTTClient handle, void * context, MQTTClient_connectionLost * cl, MQTTClient_messageArrived * ma, MQTTClient_deliveryComplete * dc) [extern]

This function sets the callback functions for a specific client. If your client application doesn't use a particular callback, set the relevant parameter to NULL (except for message arrived, which must be given). Calling MQTTClient_setCallbacks() puts the client into multi-threaded mode. Any necessary message acknowledgements and status communications are handled in the background without any intervention from the client application. See Asynchronous vs synchronous client applications for more information.

this is where you hand over your callbacks for a specific client ๐ŸŽ โ€” and if you don't use one of them, just set that parameter to NULL. the one exception? message arrived must be given. no NULL there, sorry bestie.

and the big glow-up: calling MQTTClient_setCallbacks() drops the client into multi-threaded mode, which means all the message acknowledgements and status communications get handled in the background, no intervention needed from you. it's giving "the library is handling it, you go be great" โœจ see Asynchronous vs synchronous client applications for more.

Note: the MQTT client must be disconnected when this function is called. Like, not connected. At all. Periodt.

Parameters ๐Ÿ’œ

  • handle โ€” a valid client handle from a successful call to MQTTClient_create(). Gotta have the goods.
  • context โ€” a pointer to any application-specific context. This context pointer gets passed to each of the callback functions so they can actually reach the context info inside the callback. It's giving โœจshared memoryโœจ.
  • cl โ€” a pointer to an MQTTClient_connectionLost() callback function. Set it to NULL if your application justโ€ฆ doesn't handle disconnections. (Bold choice, bestie, but okay ๐Ÿ’…)
  • ma โ€” a pointer to an MQTTClient_messageArrived() callback function. This one must be set when you call MQTTClient_setCallbacks(), because otherwise there's literally nowhere for incoming messages to land. Like a delivery with no mailbox ๐Ÿซถ
  • dc โ€” a pointer to an MQTTClient_deliveryComplete() callback function. Set it to NULL if your application publishes synchronously, or if you simply do not care to check whether delivery succeeded. Live your truth ๐ŸŒธ

Returns ๐ŸŽ€

  • MQTTCLIENT_SUCCESS if the callbacks were correctly set
  • MQTTCLIENT_FAILURE if an error occurred ๐Ÿ˜ญ

int MQTTClient_setDisconnected (MQTTClient handle, void * context, MQTTClient_disconnected * co) [extern]

Sets the MQTTClient_disconnected() callback function for a client. This one gets called if a disconnect packet is received from the server. Only valid for MQTT V5 and above โ€” no cap, older versions don't get to play.

Parameters ๐Ÿฆ‹

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • context โ€” a pointer to any application-specific context. This pointer is passed to each of the callback functions to provide access to the context information inside the callback.
  • co โ€” a pointer to an MQTTClient_disconnected() callback function. Passing NULL removes the callback setting. Poof, gone ๐Ÿ’จ

Returns โœจ

  • MQTTCLIENT_SUCCESS if the callbacks were correctly set
  • MQTTCLIENT_FAILURE if an error occurred

int MQTTClient_setPublished (MQTTClient handle, void * context, MQTTClient_published * co) [extern]

Okay so this one is literally just the signature. The manpage gives us the function and then dips โ€” no description, no parameters, no returns section. That's the whole thing. Iconic of it to show up with zero context ๐Ÿ™ˆ


int MQTTClient_create (MQTTClient * handle, const char * serverURI, const char * clientId, int persistence_type, void * persistence_context) [extern]

THE moment. This function creates an MQTT client, all ready to connect to the server you specify, using the persistent storage you specify (peep MQTTClient_persistence for that). Also go look at MQTTClient_destroy() when you're done with it โ€” clean up after yourself, queen ๐Ÿ‘‘

Parameters ๐Ÿ’–

  • handle โ€” a pointer to an MQTTClient handle. Once this function returns successfully, that handle gets populated with a valid client reference. It's giving new-baby energy ๐ŸŽ€
  • serverURI โ€” a null-terminated string saying which server the client will connect to. The form is protocol://host:port. Currently, the protocol must be one of:
    • tcp:// or mqtt:// โ€” Insecure TCP
    • ssl:// or tls:// or mqtts:// โ€” Encrypted SSL/TLS
    • ws:// โ€” Insecure websockets
    • wss:// โ€” Secure web sockets
Heads up: the TLS-enabled prefixes (ssl, tls, mqtts, wss) are only valid if a TLS version of the library is linked with. No TLS library, no TLS prefixes. Periodt.

For host, you can drop either an IP address or a host name. So like, to connect to a server running on the local machine with the default MQTT port, you'd specify tcp://localhost:1883. Simple as ๐Ÿฆ‹

  • clientId โ€” the client identifier that gets passed to the server when the client connects. It's a null-terminated UTF-8 encoded string.
  • persistence_type โ€” the type of persistence the client is gonna use:
    • MQTTCLIENT_PERSISTENCE_NONE: use in-memory persistence. If the device or system the client is running on fails or gets switched off, the current state of any in-flight messages is lost, and some messages may not be delivered even at QoS1 and QoS2. ๐Ÿ’€ Yeah. Read that again.
    • MQTTCLIENT_PERSISTENCE_DEFAULT: use the default (file system-based) persistence mechanism. Status about in-flight messages is held in persistent storage, which gives you some protection against message loss if something unexpectedly fails. Protection, not a promise ๐Ÿซถ
    • MQTTCLIENT_PERSISTENCE_USER: use an application-specific persistence implementation. This hands control of the persistence mechanism over to the application โ€” which means the application has to implement the MQTTClient_persistence interface itself. You wanted the keys, now drive ๐Ÿš—
  • persistence_context โ€” depends on the vibe:
    • If the application uses MQTTCLIENT_PERSISTENCE_NONE, this argument is unused and should be set to NULL.
    • For MQTTCLIENT_PERSISTENCE_DEFAULT, set it to the location of the persistence directory. If you set it to NULL, the persistence directory used is the working directory.
    • Applications using MQTTCLIENT_PERSISTENCE_USER set this argument to point to a valid MQTTClient_persistence structure.

Returns โญ

  • MQTTCLIENT_SUCCESS if the client is successfully created, otherwise an error code is returned.

int MQTTClient_createWithOptions (MQTTClient * handle, const char * serverURI, const char * clientId, int persistence_type, void * persistence_context, MQTTClient_createOptions * options) [extern]

A version of MQTTClient_create() that comes with additional options โ€” like the deluxe edition ๐Ÿ’… This function creates an MQTT client ready for connection to the specified server and using the specified persistent storage (see MQTTClient_persistence). See also MQTTClient_destroy().

Parameters ๐ŸŒˆ

  • handle โ€” a pointer to an MQTTClient handle. The handle is populated with a valid client reference following a successful return from this function.
  • serverURI โ€” a null-terminated string specifying the server the client will connect to. It takes the form protocol://host:port. Currently, the protocol must be tcp or ssl. For host, you can specify either an IP address or a host name. For instance, to connect to a server running on the local machines with the default MQTT port, specify tcp://localhost:1883.
  • clientId โ€” the client identifier passed to the server when the client connects to it. It is a null-terminated UTF-8 encoded string.
  • persistence_type โ€” the type of persistence to be used by the client:
    • MQTTCLIENT_PERSISTENCE_NONE: use in-memory persistence. If the device or system on which the client is running fails or is switched off, the current state of any in-flight messages is lost and some messages may not be delivered even at QoS1 and QoS2.
    • MQTTCLIENT_PERSISTENCE_DEFAULT: use the default (file system-based) persistence mechanism. Status about in-flight messages is held in persistent storage and provides some protection against message loss in the case of unexpected failure.
    • MQTTCLIENT_PERSISTENCE_USER: use an application-specific persistence implementation. Using this type of persistence gives control of the persistence mechanism to the application. The application has to implement the MQTTClient_persistence interface.
  • persistence_context โ€” if the application uses MQTTCLIENT_PERSISTENCE_NONE persistence, this argument is unused and should be set to NULL. For MQTTCLIENT_PERSISTENCE_DEFAULT persistence, it should be set to the location of the persistence directory (if set to NULL, the persistence directory used is the working directory). Applications that use MQTTCLIENT_PERSISTENCE_USER persistence set this argument to point to a valid MQTTClient_persistence structure.
  • options โ€” additional options for the create. The little extras ๐ŸŽ

Returns โœจ

  • MQTTCLIENT_SUCCESS if the client is successfully created, otherwise an error code is returned.

MQTTClient_nameValue * MQTTClient_getVersionInfo (void ) [extern]

This function returns version info about the library. No trace information will be returned โ€” just the version tea, nothing extra.

Returns ๐Ÿ‘‘

  • an array of strings describing the library. The last entry is a NULL pointer. That NULL is your stopping signal, bestie โ€” don't walk past it.

int MQTTClient_connect (MQTTClient handle, MQTTClient_connectOptions * options) [extern]

This function attempts to connect a previously-created client (see MQTTClient_create()) to an MQTT server using the options you specify. If you want asynchronous message and status notifications, you must call MQTTClient_setCallbacks() prior to MQTTClient_connect() โ€” order matters, no skipping steps ๐Ÿ’…

Parameters ๐Ÿ’œ

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • options โ€” a pointer to a valid MQTTClient_connectOptions structure.

Returns ๐ŸŽ€

  • MQTTCLIENT_SUCCESS if the client successfully connects to the server.
  • An error code is returned if the client was unable to connect to the server. Error codes greater than 0 are returned by the MQTT protocol:
 1: Connection refused: Unacceptable protocol version
 2: Connection refused: Identifier rejected
 3: Connection refused: Server unavailable
 4: Connection refused: Bad user name or password
 5: Connection refused: Not authorized
 6-255: Reserved for future use
So if you get a number back that isn't MQTTCLIENT_SUCCESS, check the chart. 1 through 5 have meanings, 6-255 are just sitting there reserved for future use โ€” do not go guessing what they do ๐Ÿ™ˆ

void MQTTResponse_free (MQTTResponse response) [extern] ๐Ÿงน

Frees the storage associated with the MQTT response. Bestie, when you're done with a response, you gotta let it go โ€” otherwise that memory just sits there being extra. ๐Ÿ’…

Parameters

  • response โ€” the response structure to be freed

MQTTResponse MQTTClient_connect5 (MQTTClient handle, MQTTClient_connectOptions * options, MQTTProperties * connectProperties, MQTTProperties * willProperties) [extern] ๐Ÿ”Œโœจ

Attempts to connect a previously-created client (see MQTTClient_create()) to an MQTT server using MQTT version 5.0 and the specified options. If you want to enable asynchronous message and status notifications, you must call MQTTClient_setCallbacks() prior to MQTTClient_connect(). no cap, do the callbacks thing first. ๐Ÿซถ

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • options โ€” A pointer to a valid MQTTClient_connectOptions structure.
  • connectProperties โ€” the MQTT 5.0 connect properties to use
  • willProperties โ€” the MQTT 5.0 properties to set on the will message

Returns

  • the MQTT 5.0 response information: error codes and properties. ๐Ÿ‘‘

int MQTTClient_disconnect (MQTTClient handle, int timeout) [extern] ๐Ÿšช

This function attempts to disconnect the client from the MQTT server. In order to allow the client time to complete handling of messages that are in-flight when this function is called, a timeout period is specified. When the timeout period has expired, the client disconnects even if there are still outstanding message acknowledgements. The next time the client connects to the same server, any QoS 1 or 2 messages which have not completed will be retried depending on the cleansession settings for both the previous and the new connection (see MQTTClient_connectOptions.cleansession and MQTTClient_connect()). it's giving "leaving the group chat but the messages still send" energy ngl ๐Ÿ˜ญ

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • timeout โ€” The client delays disconnection for up to this time (in milliseconds) in order to allow in-flight message transfers to complete.

Returns

  • MQTTCLIENT_SUCCESS if the client successfully disconnects from the server. An error code is returned if the client was unable to disconnect from the server

int MQTTClient_disconnect5 (MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties * props) [extern] ๐Ÿšช5๏ธโƒฃ


int MQTTClient_isConnected (MQTTClient handle) [extern] ๐Ÿ•ต๏ธโ™€๏ธ๐Ÿ’–

This function allows the client application to test whether or not a client is currently connected to the MQTT server. vibe check but for your connection fr fr.

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().

Returns

  • Boolean true if the client is connected, otherwise false. ๐Ÿฆ‹

int MQTTClient_subscribe (MQTTClient handle, const char * topic, int qos) [extern] ๐Ÿ“ฅ๐ŸŒธ

This function attempts to subscribe a client to a single topic, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for the subscription (see also MQTTClient_subscribeMany()).

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topic โ€” The subscription topic, which may include wildcards.
  • qos โ€” The requested quality of service for the subscription.

Returns

  • MQTTCLIENT_SUCCESS if the subscription request is successful. An error code is returned if there was a problem registering the subscription.

MQTTResponse MQTTClient_subscribe5 (MQTTClient handle, const char * topic, int qos, MQTTSubscribe_options * opts, MQTTProperties * props) [extern] ๐Ÿ“ฅ5๏ธโƒฃโœจ

This function attempts to subscribe an MQTT version 5.0 client to a single topic, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for the subscription (see also MQTTClient_subscribeMany()).

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topic โ€” The subscription topic, which may include wildcards.
  • qos โ€” The requested quality of service for the subscription.
  • opts โ€” the MQTT 5.0 subscribe options to be used
  • props โ€” the MQTT 5.0 properties to be used

Returns

  • the MQTT 5.0 response information: error codes and properties. ๐Ÿ’œ

int MQTTClient_subscribeMany (MQTTClient handle, int count, char *const * topic, int * qos) [extern] ๐Ÿ“ฅ๐Ÿ“ฅ๐Ÿ“ฅ

This function attempts to subscribe a client to a list of topics, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for each topic (see also MQTTClient_subscribe()). it's the "subscribe to everything" era and honestly? valid. ๐Ÿ”ฅ

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • count โ€” The number of topics for which the client is requesting subscriptions.
  • topic โ€” An array (of length count) of pointers to topics, each of which may include wildcards.
  • qos โ€” An array (of length count) of Quality of service values. qos[n] is the requested QoS for topic[n].

Returns

  • MQTTCLIENT_SUCCESS if the subscription request is successful. An error code is returned if there was a problem registering the subscriptions.

MQTTResponse MQTTClient_subscribeMany5 (MQTTClient handle, int count, char *const * topic, int * qos, MQTTSubscribe_options * opts, MQTTProperties * props) [extern] ๐Ÿ“ฅ5๏ธโƒฃ๐ŸŒˆ

This function attempts to subscribe an MQTT version 5.0 client to a list of topics, which may contain wildcards (see Subscription wildcards). This call also specifies the Quality of service requested for each topic (see also MQTTClient_subscribe()).

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • count โ€” The number of topics for which the client is requesting subscriptions.
  • topic โ€” An array (of length count) of pointers to topics, each of which may include wildcards.
  • qos โ€” An array (of length count) of Quality of service values. qos[n] is the requested QoS for topic[n].
  • opts โ€” the MQTT 5.0 subscribe options to be used
  • props โ€” the MQTT 5.0 properties to be used

Returns

  • the MQTT 5.0 response information: error codes and properties. โญ

int MQTTClient_unsubscribe (MQTTClient handle, const char * topic) [extern] ๐Ÿ“ค๐Ÿ“

This function attempts to remove an existing subscription made by the specified client. we love a clean unfollow. ๐ŸŽ€

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topic โ€” The topic for the subscription to be removed, which may include wildcards (see Subscription wildcards).

Returns

  • MQTTCLIENT_SUCCESS if the subscription is removed. An error code is returned if there was a problem removing the subscription.

MQTTResponse MQTTClient_unsubscribe5 (MQTTClient handle, const char * topic, MQTTProperties * props) [extern] ๐Ÿ“ค5๏ธโƒฃ

This function attempts to remove an existing subscription made by the specified client using MQTT 5.0.

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topic โ€” The topic for the subscription to be removed, which may include wildcards (see Subscription wildcards).
  • props โ€” the MQTT 5.0 properties to be used

Returns

  • the MQTT 5.0 response information: error codes and properties. ๐Ÿฆ‹

int MQTTClient_unsubscribeMany (MQTTClient handle, int count, char *const * topic) [extern] ๐Ÿ“ค๐Ÿ“ค๐Ÿ“ค

This function attempts to remove existing subscriptions to a list of topics made by the specified client. mass unfollow moment, iconic. ๐Ÿ‘‘

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • count โ€” The number subscriptions to be removed.
  • topic โ€” An array (of length count) of pointers to the topics of the subscriptions to be removed, each of which may include wildcards.

Returns

  • MQTTCLIENT_SUCCESS if the subscriptions are removed. An error code is returned if there was a problem removing the subscriptions.

MQTTResponse MQTTClient_unsubscribeMany5 (MQTTClient handle, int count, char *const * topic, MQTTProperties * props) [extern] ๐Ÿ“ค5๏ธโƒฃ๐Ÿ’œ

This function attempts to remove existing subscriptions to a list of topics made by the specified client using MQTT version 5.0.

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • count โ€” The number subscriptions to be removed.
  • topic โ€” An array (of length count) of pointers to the topics of the subscriptions to be removed, each of which may include wildcards.
  • props โ€” the MQTT 5.0 properties to be used

Returns

  • the MQTT 5.0 response information: error codes and properties. โœจ

int MQTTClient_publish (MQTTClient handle, const char * topicName, int payloadlen, const void * payload, int qos, int retained, MQTTClient_deliveryToken * dt) [extern] ๐Ÿ“ฎ๐Ÿ”ฅ

This function attempts to publish a message to a given topic (see also MQTTClient_publishMessage()). An MQTTClient_deliveryToken is issued when this function returns successfully. If the client application needs to test for succesful delivery of QoS1 and QoS2 messages, this can be done either asynchronously or synchronously (see Asynchronous vs synchronous client applications, MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()). the way that delivery token just appears when it works >>> ๐Ÿ’…

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topicName โ€” The topic associated with this message.
  • payloadlen โ€” The length of the payload in bytes.
  • payload โ€” A pointer to the byte array payload of the message.
  • qos โ€” The Quality of service of the message.
  • retained โ€” The retained flag for the message.
  • dt โ€” A pointer to an MQTTClient_deliveryToken. This is populated with a token representing the message when the function returns successfully. If your application does not use delivery tokens, set this argument to NULL.

Returns

  • MQTTCLIENT_SUCCESS if the message is accepted for publication. An error code is returned if there was a problem accepting the message. ๐ŸŒˆ

MQTTResponse MQTTClient_publish5 (MQTTClient handle, const char * topicName, int payloadlen, const void * payload, int qos, int retained, MQTTProperties * properties, MQTTClient_deliveryToken * dt) [extern] ๐Ÿ“ฎ5๏ธโƒฃโญ

Attempts to publish a message to a given topic using MQTT version 5.0 (see also MQTTClient_publishMessage5()). An MQTTClient_deliveryToken is issued when this function returns successfully. If the client application needs to test for succesful delivery of QoS1 and QoS2 messages, this can be done either asynchronously or synchronously (see Asynchronous vs synchronous client applications, MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()).

Parameters

  • handle โ€” A valid client handle from a successful call to MQTTClient_create().
  • topicName โ€” The topic associated with this message.
  • payloadlen โ€” The length of the payload in bytes.
  • payload โ€” A pointer to the byte array payload of the message.
  • qos โ€” the Quality of service of the message. ๐ŸŽš๏ธ
  • retained โ€” the retained flag for the message. ๐Ÿ“Œ
  • properties โ€” the MQTT 5.0 properties to be used. โœจ
  • dt โ€” a pointer to an MQTTClient_deliveryToken. This gets populated with a token representing the message when the function returns successfully. If your app doesn't use delivery tokens, set this argument to NULL. Bestie, just say NULL, it's fine. ๐Ÿซถ

Returns

the MQTT 5.0 response information: error codes and properties.


๐Ÿ’Œ int MQTTClient_publishMessage (MQTTClient handle, const char * topicName, MQTTClient_message * msg, MQTTClient_deliveryToken * dt) [extern]

Okay so this one tries to publish a message to a given topic (peep also MQTTClient_publish()). An MQTTClient_deliveryToken is issued when this function returns successfully. And if your client app needs to test for succesful delivery of QoS1 and QoS2 messages, you can do that either asynchronously or synchronously (go look at Asynchronous vs synchronous client applications, MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()). โœจ

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • topicName โ€” the topic associated with this message.
  • msg โ€” a pointer to a valid MQTTClient_message structure containing the payload and attributes of the message to be published.
  • dt โ€” a pointer to an MQTTClient_deliveryToken. This is populated with a token representing the message when the function returns successfully. If your application does not use delivery tokens, set this argument to NULL.

Returns

MQTTCLIENT_SUCCESS if the message is accepted for publication. An error code is returned if there was a problem accepting the message. ๐Ÿ’…


๐ŸŒˆ MQTTResponse MQTTClient_publishMessage5 (MQTTClient handle, const char * topicName, MQTTClient_message * msg, MQTTClient_deliveryToken * dt) [extern]

Attempts to publish a message to the given topic using MQTT version 5.0 (see also MQTTClient_publish5()). An MQTTClient_deliveryToken is issued when this function returns successfully. If the client application needs to test for succesful delivery of QoS1 and QoS2 messages, this can be done either asynchronously or synchronously (see Asynchronous vs synchronous client applications, MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()). Same energy as its sibling, just v5 flavoured. ๐Ÿ’–

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • topicName โ€” the topic associated with this message.
  • msg โ€” a pointer to a valid MQTTClient_message structure containing the payload and attributes of the message to be published.
  • dt โ€” a pointer to an MQTTClient_deliveryToken. This is populated with a token representing the message when the function returns successfully. If your application does not use delivery tokens, set this argument to NULL.

Returns

the MQTT 5.0 response information: error codes and properties.


โณ int MQTTClient_waitForCompletion (MQTTClient handle, MQTTClient_deliveryToken dt, unsigned long timeout) [extern]

Your client app calls this to sync up the main thread with the completed publication of a message. The moment you call it, MQTTClient_waitForCompletion() blocks execution until the message has been successful delivered or the timeout you specified has expired. See Asynchronous vs synchronous client applications. Yes queen, it will just sit there and wait. That's the whole job. ๐Ÿ›‘

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • dt โ€” the MQTTClient_deliveryToken that represents the message being tested for successful delivery. Delivery tokens are issued by the publishing functions MQTTClient_publish() and MQTTClient_publishMessage().
  • timeout โ€” the maximum time to wait in milliseconds.

Returns

MQTTCLIENT_SUCCESS if the message was successfully delivered. An error code is returned if the timeout expires or there was a problem checking the token. ๐Ÿ˜ญ


๐ŸŽ€ int MQTTClient_getPendingDeliveryTokens (MQTTClient handle, MQTTClient_deliveryToken ** tokens) [extern]

This function sets a pointer to an array of delivery tokens for messages that are currently in-flight (pending completion). Little snapshot of the chaos. ๐Ÿ“ธ

Important note: The memory used to hold the array of tokens is malloc()'d in this function. The client application is responsible for freeing this memory when it is no longer required. Do not forget. I am watching. ๐Ÿ‘€

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • tokens โ€” the address of a pointer to an MQTTClient_deliveryToken. When the function returns successfully, the pointer is set to point to an array of tokens representing messages pending completion. The last member of the array is set to -1 to indicate there are no more tokens. If no tokens are pending, the pointer is set to NULL.

Returns

MQTTCLIENT_SUCCESS if the function returns successfully. An error code is returned if there was a problem obtaining the list of pending tokens.


๐Ÿฆ‹ void MQTTClient_yield (void ) [extern]

When implementing a single-threaded client, call this function periodically to allow processing of message retries and to send MQTT keepalive pings. If the application is calling MQTTClient_receive() regularly, then it is not necessary to call this function. So likeโ€ฆ it's one or the other, bestie, don't do both. ๐Ÿ’…


๐Ÿ“ฅ int MQTTClient_receive (MQTTClient handle, char ** topicName, int * topicLen, MQTTClient_message ** message, unsigned long timeout) [extern]

This function performs a synchronous receive of incoming messages. It should be used only when the client application has not set callback methods to support asynchronous receipt of messages (see Asynchronous vs synchronous client applications and MQTTClient_setCallbacks()). Using this function allows a single-threaded client subscriber application to be written. When called, this function blocks until the next message arrives or the specified timeout expires (see also MQTTClient_yield()). Sit. Wait. Receive. Iconic behaviour, no notes. ๐Ÿง˜

Important note: The application must free() the memory allocated to the topic and the message when processing is complete (see MQTTClient_freeMessage()). No leaks in this household. ๐Ÿšซ

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • topicName โ€” the address of a pointer to a topic. This function allocates the memory for the topic and returns it to the application by setting topicName to point to the topic.
  • topicLen โ€” the length of the topic. If the return code from this function is MQTTCLIENT_TOPICNAME_TRUNCATED, the topic contains embedded NULL characters and the full topic should be retrieved by using topicLen. Sneaky little NULLs. ๐Ÿ™ˆ
  • message โ€” the address of a pointer to the received message. This function allocates the memory for the message and returns it to the application by setting message to point to the received message. The pointer is set to NULL if the timeout expires.
  • timeout โ€” the length of time to wait for a message in milliseconds.

Returns

MQTTCLIENT_SUCCESS or MQTTCLIENT_TOPICNAME_TRUNCATED if a message is received. MQTTCLIENT_SUCCESS can also indicate that the timeout expired, in which case message is NULL. An error code is returned if there was a problem trying to receive a message. Yeah, MQTTCLIENT_SUCCESS doing double duty, we love a multitasker. ๐Ÿ‘‘


๐Ÿงน void MQTTClient_freeMessage (MQTTClient_message ** msg) [extern]

This function frees memory allocated to an MQTT message, including the additional memory allocated to the message payload. The client application calls this function when the message has been fully processed.

Important note: This function does not free the memory allocated to a message topic string. It is the responsibility of the client application to free this memory using the MQTTClient_free() library function. Two separate cleanups, bestie. Do them both. ๐Ÿซถ

Parameters

  • msg โ€” the address of a pointer to the MQTTClient_message structure to be freed.

๐Ÿ—‘๏ธ void MQTTClient_free (void * ptr) [extern]

This function frees memory allocated by the MQTT C client library, especially the topic name. This is needed on Windows when the client libary and application program have been compiled with different versions of the C compiler. It is thus good policy to always use this function when freeing any MQTT C client-allocated memory. Windows, you chaotic little thing. ๐Ÿ’€

Parameters

  • ptr โ€” the pointer to the client library storage to be freed.

๐ŸŒธ void * MQTTClient_malloc (size_t size) [extern]

This function is used to allocate memory to be used or freed by the MQTT C client library, especially the data in user persistence. This is needed on Windows when the client library and application program have been compiled with different versions of the C compiler. Same Windows situation, but make it allocation. ๐ŸชŸ

Parameters

  • size โ€” the size of the memory to be allocated.

๐Ÿ’” void MQTTClient_destroy (MQTTClient * handle) [extern]

This function frees the memory allocated to an MQTT client (see MQTTClient_create()). It should be called when the client is no longer required. It's the breakup function. Say your goodbyes, free your handle. ๐Ÿ˜ญ

Parameters

  • handle โ€” a pointer to the handle referring to the MQTTClient structure to be freed.

๐Ÿ” void MQTTClient_setTraceLevel (enum MQTTCLIENT_TRACE_LEVELS level) [extern]

This function sets the level of trace information which will be returned in the trace callback.

Parameters

  • level โ€” the trace level required

๐Ÿ“ก void MQTTClient_setTraceCallback (MQTTClient_traceCallback * callback) [extern]

This function sets the trace callback if needed. If set to NULL, no trace information will be returned. The default trace level is MQTTASYNC_TRACE_MINIMUM. Set it to NULL and it goes fully silent, like a ghost. ๐Ÿ‘ป

Parameters

  • callback โ€” a pointer to the function which will handle the trace information

โฐ int MQTTClient_setCommandTimeout (MQTTClient handle, unsigned long milliSeconds) [extern]

Sets the timeout value for un/subscribe commands when waiting for the un/suback response from the server. Values less than 5000 are not allowed. Don't try it. The library said what it said. ๐Ÿšซ

Parameters

  • handle โ€” a valid client handle from a successful call to MQTTClient_create().
  • milliSeconds โ€” the maximum number of milliseconds to wait

Returns

MQTTCLIENT_SUCCESS or MQTTCLIENT_FAILURE


๐Ÿ’ฌ const char * MQTTClient_strerror (int code) [extern]

Returns a pointer to the string representation of the error or NULL. Translation service for your error codes, so slay. ๐Ÿซถ

Do not free after use. Returns NULL if the error code is unknown. Do not free it. I said what I said. ๐Ÿ’…


โœจ Author โœจ

Generated automatically by Doxygen for Paho MQTT C Client Library from the source code. Not a human, just Doxygen doing its little robot thing. ๐Ÿค–


And that's the whole page, bestie!! ๐Ÿ’– This was Paho MQTT C Client Library, the MQTTClient.h(3) manual page, and it was generated on Tue Jun 2 2026 00:00:00. Go forth and publish responsibly. โœจ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from