libmosquitto (3) - Linux Manuals
libmosquitto: MQTT version 3.1 client library
NAME
libmosquitto - MQTT version 3.1 client library
DESCRIPTION
This is an overview of how to use libmosquitto to create MQTT aware client programs. There may be separate man pages on each of the functions described here in the future. There is also a binding for libmosquitto for C++ and a Python implementation. They are not documented here but operate in a similar way.
This is fairly incomplete, please see mosquitto.h for a better description of the functions.
LIBMOSQUITTO SYMBOL NAMES
All public functions in libmosquitto have the prefix "mosquitto_". Any other functions defined in the source code are to be treated as private functions and may change between any release. Do not use these functions!
FUNCTIONS
Library version
-
int mosquitto_lib_version(int
* major, int* minor, int* revision);
Library initialisation and cleanup
-
int mosquitto_lib_init(void);
- int mosquitto_lib_cleanup(void);
Client constructor/destructor
-
struct mosquitto *mosquitto_new(const char
* id, boolclean_session, void * userdata);
-
void mosquitto_destroy(struct mosquitto
* mosq);
-
int mosquitto_reinitialise(struct mosquitto
* mosq, const char* id, boolclean_session, void * userdata);
Authentication and encryption
-
int mosquitto_username_pw_set(struct mosquitto
* mosq, const char* username, const char* password);- int mosquitto_tls_set(struct mosquitto
* mosq, const char* cafile, const char* capath, const char* certfile, const char* keyfile, int(*pw_callback)(char *buf, int size, int rwflag, void );*userdata) - int mosquitto_tls_opts_set(struct mosquitto
* mosq, intcert_reqs, const char * tls_version, const char* ciphers);- int mosquitto_tls_insecure_set(struct mosquitto
* mosq, boolvalue); - int mosquitto_tls_psk_set(struct mosquitto
* mosq, const char* psk, const char* identity, const char* ciphers); - int mosquitto_tls_set(struct mosquitto
Wills
-
int mosquitto_will_set(struct mosquitto
* mosq, const char* topic, intpayloadlen, const void * payload, intqos, bool retain); - int mosquitto_will_clear(struct mosquitto
*mosq ); - int mosquitto_will_clear(struct mosquitto
Connect/disconnect
-
int mosquitto_connect(struct mosquitto
* mosq, const char* host, intport, int keepalive); - int mosquitto_connect_bind(struct mosquitto
* mosq, const char* host, intport, int keepalive, const char * bind_address);- int mosquitto_connect_async(struct mosquitto
* mosq, const char* host, intport, int keepalive); - int mosquitto_connect_bind_async(struct mosquitto
* mosq, const char* host, intport, int keepalive, const char * bind_address);- int mosquitto_reconnect(struct mosquitto
* mosq);- int mosquitto_reconnect_async(struct mosquitto
* mosq);- int mosquitto_disconnect(struct mosquitto
* mosq); - int mosquitto_connect_bind(struct mosquitto
Publish
-
int mosquitto_publish(struct mosquitto
* mosq, int* mid, const char* topic, intpayloadlen, const void * payload, intqos, bool retain);
Subscribe/unsubscribe
-
int mosquitto_subscribe(struct mosquitto
* mosq, int* mid, const char* sub, intqos); - int mosquitto_unsubscribe(struct mosquitto
* mosq, int* mid, const char* sub); - int mosquitto_unsubscribe(struct mosquitto
Network loop
-
int mosquitto_loop(struct mosquitto
* mosq, inttimeout, int max_packets); - int mosquitto_loop_read(struct mosquitto
* mosq, intmax_packets); - int mosquitto_loop_write(struct mosquitto
* mosq, intmax_packets); - int mosquitto_loop_misc(struct mosquitto
* mosq);- int mosquitto_loop_forever(struct mosquitto
* mosq, inttimeout, int max_packets); - int mosquitto_socket(struct mosquitto
* mosq);- bool mosquitto_want_write(struct mosquitto
* mosq); - int mosquitto_loop_read(struct mosquitto
Threaded network loop
-
int mosquitto_loop_start(struct mosquitto
* mosq);- int mosquitto_loop_stop(struct mosquitto
* mosq, boolforce); - int mosquitto_loop_stop(struct mosquitto
Misc client functions
-
int mosquitto_max_inflight_messages_set(struct mosquitto
* mosq, unsigned intmax_inflight_messages); - int mosquitto_message_retry_set(struct mosquitto
* mosq, unsigned intmessage_retry); - int mosquitto_reconnect_delay_set(struct mosquitto
* mosq, unsigned intreconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff); - int mosquitto_user_data_set(struct mosquitto
* mosq, void* userdata); - int mosquitto_message_retry_set(struct mosquitto
Callbacks
-
int mosquitto_connect_callback_set(struct mosquitto
* mosq, void(*on_connect)(struct mosquitto *, void );*, int) - int mosquitto_disconnect_callback_set(struct mosquitto
* mosq, void(*on_disconnect)(struct mosquitto *, void );*, int) - int mosquitto_publish_callback_set(struct mosquitto
* mosq, void(*on_publish)(struct mosquitto *, void );*, int) - int mosquitto_message_callback_set(struct mosquitto
* mosq, void(*on_message)(struct mosquitto *, void );*, const struct mosquitto_message *) - int mosquitto_subscribe_callback_set(struct mosquitto
* mosq, void(*on_subscribe)(struct mosquitto *, void );*, int, int, const int *) - int mosquitto_unsubscribe_callback_set(struct mosquitto
* mosq, void(*on_unsubscribe)(struct mosquitto *, void );*, int) - int mosquitto_log_callback_set(struct mosquitto
* mosq, void(*on_unsubscribe)(struct mosquitto *, void );*, int, const char *) - int mosquitto_disconnect_callback_set(struct mosquitto
Utility functions
-
const char *mosquitto_connack_string(int
connack_code); - int mosquitto_message_copy(struct mosquitto_message
* dst, const struct mosquitto_message* src);- int mosquitto_message_free(struct mosquitto_message
** message);- const char *mosquitto_strerror(int
mosq_errno); - int mosquitto_sub_topic_tokenise(const char
* subtopic, char*** topics, int* count);- int mosquitto_sub_topic_tokens_free(char
*** topics, intcount); - int mosquitto_topic_matches_sub(const char
* sub, const char* topic, bool* result); - int mosquitto_message_copy(struct mosquitto_message
EXAMPLES
-
#include <stdio.h> #include <mosquitto.h>
- void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { if(message->payloadlen){ printf("%s %s\n", message->topic, message->payload); }else{ printf("%s (null)\n", message->topic); } fflush(stdout); } void my_connect_callback(struct mosquitto *mosq, void *userdata, int result) { int i; if(!result){ /* Subscribe to broker information topics on successful connect. */ mosquitto_subscribe(mosq, NULL, "$SYS/#", 2); }else{ fprintf(stderr, "Connect failed\n"); } } void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos) { int i; printf("Subscribed (mid: %d): %d", mid, granted_qos[0]); for(i=1; i<qos_count; i++){ printf(", %d", granted_qos[i]); } printf("\n"); } void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str) { /* Pring all log messages regardless of level. */ printf("%s\n", str); } int main(int argc, char *argv[]) { int i; char *host = "localhost"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
AUTHOR
Roger Light <roger [at] atchoo.org>