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 ADDRESS as "tcp://test.mosquitto.org:1883" β€” that's the broker it's talking to πŸ’…
  • CLIENTID is "ExampleClientPub", TOPIC is "MQTT Examples", and the PAYLOAD is, iconic behaviour, "Hello World!" ✨
  • QOS is 1 and TIMEOUT is 10000L β€” that's 10 seconds of patience, periodt
  • it creates the client with MQTTClient_create, using MQTTCLIENT_PERSISTENCE_NONE and NULL for the persistence context
  • conn_opts.keepAliveInterval = 20; and conn_opts.cleansession = 1; β€” then it connects with MQTTClient_connect
  • it fills in pubmsg.payload, pubmsg.payloadlen, pubmsg.qos and sets pubmsg.retained = 0; before firing off MQTTClient_publishMessage
  • then the waiting era begins: MQTTClient_waitForCompletion(client, token, TIMEOUT) 🫢
  • every failure path prints a "Failed to ..." message with the rc and calls exit(EXIT_FAILURE); β€” MQTTClient_disconnect is the exception, it just prints instead of exiting, and then MQTTClient_destroy(&client); cleans up and return 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