pubasync(3)
๐ NAME ๐
okay bestie, sit down, grab ur boba ๐ง because pubasync is literally the Asynchronous publication example โ that's the whole job description, no cap. It's a full little C program from the Library Functions Manual that shows u how to publish a message with the Paho MQTT C client and then wait for the delivery token to come home ๐๏ธโจ
the vibe is: create a client, hook up callbacks, connect, publish, then spin in a loop until the message u sent is confirmed delivered. main character energy fr fr ๐
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#define ADDRESS "tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
MQTTClient_deliveryToken deliveredtoken;
void delivered(void *context, MQTTClient_deliveryToken dt)
{
printf("Message with token value %d delivery confirmed\n", dt);
deliveredtoken = dt;
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
void connlost(void *context, char *cause)
{
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
}
int main(int argc, char* argv[])
{
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
goto exit;
}
if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to set callbacks, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = (int)strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
deliveredtoken = 0;
if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to publish message, return code %d\n", rc);
rc = EXIT_FAILURE;
}
else
{
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while (deliveredtoken != token)
{
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
}
}
if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
}
destroy_exit:
MQTTClient_destroy(&client);
exit:
return rc;
}
๐ธ the grand tour, step by step ๐ธ
the includes, the platform check, and the glow-up macros
#include <stdio.h>,#include <stdlib.h>,#include <string.h>and#include "MQTTClient.h"come in first, in that order ๐- then a little OS vibe check:
#if !defined(_WIN32)pulls in#include <unistd.h>, and the#elsebranch pulls in#include <windows.h>, closed off with#endif๐ช ADDRESSis defined as"tcp://test.mosquitto.org:1883"CLIENTIDis defined as"ExampleClientPub"TOPICis defined as"MQTT Examples"PAYLOADis defined as"Hello World!"QOSis defined as1TIMEOUTis defined as10000L
the global bestie
MQTTClient_deliveryToken deliveredtoken;sits at file scope, being the shared little flag the callbacks andmainboth look at ๐
delivered() โ signature void delivered(void *context, MQTTClient_deliveryToken dt)
- prints
"Message with token value %d delivery confirmed\n"withdtfilled in - then sets
deliveredtoken = dt;
msgarrvd() โ signature int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
- prints
"Message arrived\n" - prints
" topic: %s\n"withtopicName - prints
" message: %.*s\n"usingmessage->payloadlenand(char*)message->payload - then cleans up with
MQTTClient_freeMessage(&message);andMQTTClient_free(topicName); - and
return 1;๐
connlost() โ signature void connlost(void *context, char *cause)
- prints
"\nConnection lost\n" - prints
" cause: %s\n"withcause๐ญ
main() โ signature int main(int argc, char* argv[])
locals, in order: MQTTClient client;, MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;, MQTTClient_message pubmsg = MQTTClient_message_initializer;, MQTTClient_deliveryToken token;, int rc;
then the actual plot:
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)โ if the return code isn'tMQTTCLIENT_SUCCESS, it prints"Failed to create client, return code %d\n"withrc, setsrc = EXIT_FAILURE;, and doesgoto exit;MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)โ if that isn'tMQTTCLIENT_SUCCESS, it prints"Failed to set callbacks, return code %d\n"withrc, setsrc = EXIT_FAILURE;, and doesgoto destroy_exit;conn_opts.keepAliveInterval = 20;andconn_opts.cleansession = 1;, thenMQTTClient_connect(client, &conn_opts)โ if that isn'tMQTTCLIENT_SUCCESS, it prints"Failed to connect, return code %d\n"withrc, setsrc = EXIT_FAILURE;, and doesgoto destroy_exit;- then it stuffs the message:
pubmsg.payload = PAYLOAD;,pubmsg.payloadlen = (int)strlen(PAYLOAD);,pubmsg.qos = QOS;,pubmsg.retained = 0;,deliveredtoken = 0;- then
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)โ if that's notMQTTCLIENT_SUCCESS, it prints"Failed to publish message, return code %d\n"withrcand setsrc = EXIT_FAILURE; - the
elsebranch is the cute part: it prints"Waiting for publication of %s\n"and"on topic %s for client with ClientID: %s\n"passingPAYLOAD,TOPIC,CLIENTID, and then waits โwhile (deliveredtoken != token)loops forever until the delivered token catches up to the token we got back, and inside that loop it doesSleep(100);under#if defined(_WIN32)andusleep(10000L);under the#else๐ฆ
- then
- then
MQTTClient_disconnect(client, 10000)โ if that isn'tMQTTCLIENT_SUCCESS, it prints"Failed to disconnect, return code %d\n"withrcand setsrc = EXIT_FAILURE; destroy_exit:label โMQTTClient_destroy(&client);exit:label โreturn rc;
bestie listen โ that while (deliveredtoken != token) loop has no timeout and no exit, so it will just keep spinning until the token arrives. that's the "asynchronous publication" bit doing its thing. don't say I didn't warn u ๐
and that's the whole page, periodt ๐ this lil beauty comes to u from the Paho MQTT C Client Library, dated Tue Jun 2 2026 00:00:00 โ stan a classic, mwah ๐ซถ๐โจ
๐ฆ where this page comes from
- fedora 44: paho-c-devel 1.3.16-1.fc44 ยท BSD-3-Clause AND EPL-2.0
- fedora 44: paho-c-devel 1.3.16-1.fc44 ยท BSD-3-Clause AND EPL-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms