Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] Question about publishing to an MQTT brokerusing TLS

Hi Mark,

mosquitto_publish() doesn't block waiting until the message is sent,
so your program will exit very quickly.

You should call one of mosquitto_loop_start() or
mosquitto_loop_forever() in order that the network traffic actually
gets processed.

Cheers,

Roger



On Wed, Jan 27, 2016 at 1:30 PM, Mark Pilant <lpilant@xxxxxxxxxx> wrote:
> Hi Roger.
>
> I did call mosquitto_lib_init(). Since the test program isn't very long I've
> included it below.
>
> I included the sleep() call because I thought the callbacks would be called
> asynchronously, and wanted to see if I was actually getting the connect
> completion before doing the publish. I notice pub_client.c actually does the
> mosquitto_publish from within the connect callback. I expect I'll so
> something similar to avoid races, but I didn't make that change because I
> noticed the connect wasn't completing.
>
> - Mark
>
> ---------------------------------------------------------------------------------------------
>
> #include <stdio.h>
> #include <string.h>
>
> #include <mosquitto.h>
>
> #define cafile "./rootCA.pem"
> #define certfile "./cert.pem"
> #define keyfile "./private-key.pem"
> #define host "the-mqtt-broker.com"
> #define port 8883
> #define qos 0
> #define topic "topic/test"
> #define message "{\"msg\" : \"Hello ... World\"}"
>
> void connect_callback(struct mosquitto *mosq, void *obj, int result) {
> printf("Connect callback: \n");
> }
>
> void disconnect_callback(struct mosquitto *mosq, void *obj, int rc) {
> printf("Disconnect callback: \n");
> }
>
> void log_callback(struct mosquitto *mosq, void *obj, int level, const char
> *str) {
> printf("%s\n", str);
> }
>
> void publish_callback(struct mosquitto *mosq, void *obj, int mid) {
> printf("Publish callback: \n");
> }
>
> int main (int argc, char *argv[]) {
> char idstr[64];
> struct mosquitto *mosq = NULL;
> // int mqtt_version = 3; /* MQTT_PROTOCOL_V31 */
> int mqtt_version = 4; /* MQTT_PROTOCOL_V311 */
> int status;
>
> /* Initialize needed storage. */
> snprintf(idstr, sizeof(idstr), "mosquitto_%d", getpid());
>
> /* Initialize mosquitto. */
> mosquitto_lib_init();
>
> /* Create a new mosquitto instance. */
> mosq = mosquitto_new(idstr, true, NULL);
> if (!mosq) {
> printf("Error creating mosquitto instance\n");
> return 1;
> }
> printf("New mosquitto instance created\n");
>
> /* Connect up all the callback functions. */
> mosquitto_log_callback_set(mosq, log_callback);
> mosquitto_connect_callback_set(mosq, connect_callback);
> mosquitto_disconnect_callback_set(mosq, disconnect_callback);
> mosquitto_publish_callback_set(mosq, publish_callback);
> printf("Callback setup complete\n");
>
> /* Use TLS for communications. */
> status = mosquitto_tls_set(mosq, cafile, NULL,
> certfile, keyfile, NULL);
> if (status) {
> printf("Error (%d) configuring TLS.\n", status);
> return 2;
> }
> printf("TLS setup complete\n");
>
> /* Maximum inflight messages. */
> status = mosquitto_max_inflight_messages_set(mosq, 20);
> if (status) {
> printf("Error (%d) setting max inflight message count.\n", status);
> return 6;
> }
> printf("Max inflight count set\n");
>
> /* Set mosquitto options. */
> status = mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION,
> &mqtt_version);
> if (status) {
> printf("Error (%d) setting mosquitto options.\n", status);
> return 7;
> }
> printf("Mosquitto options set\n");
>
> /* Connect to the broker. */
> status = mosquitto_connect_bind(mosq, host, port, 60, NULL);
> if (status) {
> printf("Error (%d) connecting to broker.\n", status);
> return 3;
> }
>
> sleep(1000);
>
> /* Publish the message */
> status = mosquitto_publish(mosq, NULL, topic,
> strlen(message), message,
> qos, false);
> if (status) {
> printf("Error (%d) publishing message.\n", status);
> return 4;
> }
>
> return 0;
> }
>
>
>
> _______________________________________________
> mosquitto-dev mailing list
> mosquitto-dev@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://dev.eclipse.org/mailman/listinfo/mosquitto-dev


Back to the top