subasync(3)
✨ NAME ✨
subasync — Asynchronous subscription example 🦋
okay so bestie, sit down, hydrate, because this page is subasync(3) — it lives in the Library Functions Manual (section 3, we're fancy like that 💅). But real talk: this is not a function you call. It's a whole entire example program, a working MQTT client that subscribes to a topic asynchronously, and it's giving main character energy from the first #include to the last return. No cap. 💖
🎀 the whole program, in all its glory 🎀
First it pulls in the headers — <stdio.h>, <stdlib.h>, <string.h>, and the icon itself, "MQTTClient.h" — then it declares its constants with #define: the broker ADDRESS is "tcp://test.mosquitto.org:1883", the CLIENTID is "ExampleClientSub", the TOPIC is "MQTT Examples", the PAYLOAD is "Hello World!", QOS is 1, and TIMEOUT is 10000L. Then there's a volatile MQTTClient_deliveryToken deliveredtoken; sitting at file scope like it owns the place. 👑
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
volatile 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;
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;
}
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
if ((rc = MQTTClient_subscribe(client, TOPIC, QOS)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to subscribe, return code %d\n", rc);
rc = EXIT_FAILURE;
}
else
{
int ch;
do
{
ch = getchar();
} while (ch!='Q' && ch != 'q');
if ((rc = MQTTClient_unsubscribe(client, TOPIC)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to unsubscribe, return code %d\n", rc);
rc = EXIT_FAILURE;
}
}
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 cast of characters (iykyk) 🧚
delivered— the callback that fires when a delivery is confirmed. It printsMessage with token value %d delivery confirmedwithdt, then stashesdtintodeliveredtoken. Signature:void delivered(void *context, MQTTClient_deliveryToken dt). ✨msgarrvd— the moment a message shows up, this one announcesMessage arrived, prints thetopicand themessage(usingmessage->payloadlenand(char*)message->payload), then frees the message withMQTTClient_freeMessage(&message)and the topic name withMQTTClient_free(topicName), and returns1. Signature:int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message). 🎀connlost</> — dramatic but necessary: it printsConnection lostand thecause. Signature:void connlost(void *context, char *cause). 😭main—int main(int argc, char* argv[]), the whole plot. It makes aMQTTClient client;and aMQTTClient_connectOptions conn_optsinitialized toMQTTClient_connectOptions_initializer, plus anint rc;to hold return codes. 🌈
🫶 what main actually does, step by step 🫶
- Calls
MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL). If it doesn't equalMQTTCLIENT_SUCCESS, it printsFailed to create client, return code %d, setsrc = EXIT_FAILURE, and jumps toexit. 💀 - Calls
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered). On failure: printsFailed to set callbacks, return code %d, setsrc = EXIT_FAILURE, jumps todestroy_exit. - Sets
conn_opts.keepAliveInterval = 20;andconn_opts.cleansession = 1;, then callsMQTTClient_connect(client, &conn_opts). On failure: printsFailed to connect, return code %d, setsrc = EXIT_FAILURE, jumps todestroy_exit. - Prints
Subscribing to topic %s\nfor client %s using QoS%d\n\nandPress Q<Enter> to quit\n\nwithTOPIC,CLIENTID,QOS, then callsMQTTClient_subscribe(client, TOPIC, QOS). If that fails it printsFailed to subscribe, return code %dand setsrc = EXIT_FAILURE. - Otherwise it loops on
getchar()—do { ch = getchar(); } while (ch!='Q' && ch != 'q');— so you can bounce whenever you're done. Then it callsMQTTClient_unsubscribe(client, TOPIC), and if that's notMQTTCLIENT_SUCCESSit printsFailed to unsubscribe, return code %dand setsrc = EXIT_FAILURE. 👋 - Calls
MQTTClient_disconnect(client, 10000), printingFailed to disconnect, return code %dand settingrc = EXIT_FAILUREif it doesn't returnMQTTCLIENT_SUCCESS. - The
destroy_exit:label runsMQTTClient_destroy(&client);, theexit:label runsreturn rc;, and that's the whole story, periodt. 💅
bestie reminder: this page is the example, so the real magic lives in the MQTTClient functions it calls — this is your blueprint, not the engine. 🔥
and that's the tea 🍓 this gem ships with the Paho MQTT C Client Library, dated Tue Jun 2 2026 00:00:00 — go be asynchronous, you absolute icon 👑💜
📦 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