pubsync(3)
β¨ NAME β¨
pubsync β bestie, this is the Synchronous publication example π it's from the Library Functions Manual, and it's literally the whole pubsync(3) page.
okay so real talk: this page is basically one (1) big code block, and it's a full working MQTT publish demo that connects to a broker, publishes "Hello World!" on a topic, waits for the delivery to actually land, then bails out gracefully. no cap, it's the glow-up version of "hello world" for MQTT πΈ
it shows you the synchronous vibe β meaning after you publish, you sit there and wait (up to TIMEOUT ms) for the delivery token to come back confirmed. it's giving patient main character energy π
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "tcp://test.mosquitto.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
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);
exit(EXIT_FAILURE);
}
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);
exit(EXIT_FAILURE);
}
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = (int)strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to publish message, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
(int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
printf("Failed to disconnect, return code %d\n", rc);
MQTTClient_destroy(&client);
return rc;
}
so what's actually happening in there, bestie? let's break it down π¦
- it defines
ADDRESSas"tcp://test.mosquitto.org:1883"β that's the broker it's talking to π CLIENTIDis"ExampleClientPub",TOPICis"MQTT Examples", and thePAYLOADis, iconic behaviour,"Hello World!"β¨QOSis1andTIMEOUTis10000Lβ that's 10 seconds of patience, periodt- it creates the client with
MQTTClient_create, usingMQTTCLIENT_PERSISTENCE_NONEandNULLfor the persistence context conn_opts.keepAliveInterval = 20;andconn_opts.cleansession = 1;β then it connects withMQTTClient_connect- it fills in
pubmsg.payload,pubmsg.payloadlen,pubmsg.qosand setspubmsg.retained = 0;before firing offMQTTClient_publishMessage - then the waiting era begins:
MQTTClient_waitForCompletion(client, token, TIMEOUT)π«Ά - every failure path prints a
"Failed to ..."message with thercand callsexit(EXIT_FAILURE);βMQTTClient_disconnectis the exception, it just prints instead of exiting, and thenMQTTClient_destroy(&client);cleans up andreturn rc;sends you on your way π
heads up bestie π the#includes, the#defines, the struct initializers and literally every line of that code are here exactly as written β don't go renaming stuff or "fixing" it, it's canon π₯
ngl this is the cutest little MQTT starter you'll ever meet, and it ate π no cap. go forth and publish, my love π
kisses from the Paho MQTT C Client Library, dated Tue Jun 2 2026 00:00:00 β that's the source and the date, 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