REW

What Is Mosquitto Library?

Published Aug 29, 2025 4 min read
On this page

Eclipse Mosquitto provides an open-source C library, libmosquitto, for building MQTT clients. MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for machine-to-machine (M2M) and Internet of Things (IoT) communication. The Mosquitto client library allows developers to integrate the MQTT publish/subscribe messaging model into their applications, enabling them to connect to an MQTT broker and exchange messages efficiently.

The broader Mosquitto project includes not only the client library but also a popular open-source MQTT broker. The library is specifically for creating client-side applications that interact with a broker, which can be the Mosquitto broker or any other MQTT-compliant broker.

Core concepts and functionality

The Mosquitto library and the MQTT protocol are built around a few core concepts:

  • Publish/Subscribe: This is the fundamental messaging pattern. Instead of sending messages directly to a recipient (point-to-point), a client publishes a message to a named topic, and any client subscribed to that topic will receive the message.
  • Topics: These are UTF-8 strings that function as virtual channels for messages. They are hierarchical, with levels separated by a forward slash (e.g., house/living_room/temperature).
  • Broker: The central server that receives all messages, filters them by topic, and sends them to all subscribed clients. The Mosquitto library is used for creating the clients that connect to this broker.
  • Quality of Service (QoS): The library supports the three levels of QoS defined by the MQTT protocol to guarantee message delivery reliability.
    • QoS 0 (At most once): The broker delivers a message without any delivery confirmation.
    • QoS 1 (At least once): The broker and client engage in a handshake to ensure the message is delivered at least once, with the possibility of duplicates.
    • QoS 2 (Exactly once): A four-part handshake guarantees the message is delivered to the recipient exactly one time, with no duplicates.
  • Persistence: Mosquitto supports message persistence, where the broker can store messages for disconnected clients and deliver them once the clients reconnect.

Features of the Mosquitto client library

The C library, libmosquitto, and its wrappers for other languages offer a robust set of features for developers:

  • Cross-platform compatibility: The library is highly portable and can be used on various operating systems, including Windows, Linux, macOS, and embedded systems.
  • Lightweight and efficient: Designed to be minimal and use low bandwidth, the library is ideal for resource-constrained devices like IoT sensors and microcontrollers.
  • Asynchronous API: The library's event-driven, callback-based API handles network interactions efficiently, allowing your application to perform other tasks while waiting for messages.
  • TLS/SSL support: For secure connections, the library includes support for TLS/SSL encryption. This is crucial for applications that handle sensitive data.
  • Authentication support: Basic authentication with a username and password is a standard feature. For enhanced security, it also supports different authentication schemes through plugins in MQTT v5.
  • Language bindings: In addition to the core C library, wrappers and client libraries exist for a wide range of programming languages, including:
    • C++: The mosquittopp C++ wrapper simplifies the library's use.
    • Python: The Eclipse Paho project provides a popular and actively maintained Python client that can be installed via pip.
  • MQTT v5 support: The library fully supports the latest features of the MQTT v5 protocol, including:
    • Enhanced authentication
    • Improved error handling with reason codes
    • Flow control
    • Shared subscriptions for load balancing

How developers use the Mosquitto library

To build an application with the Mosquitto client library, developers typically follow these steps:

  1. Initialize the library: The application must first call a function, such as mosquitto_lib_init(), to set up internal library resources.
  2. Create a client instance: A new client object is instantiated and given a unique client ID.
  3. Set up callbacks: The application defines callback functions to handle different events, such as when a message is received (on_message), a connection is established (on_connect), or a subscription is successful (on_subscribe).
  4. Connect to a broker: The client connects to the MQTT broker using its address and port.
  5. Subscribe or publish: The client can then subscribe to topics to receive messages or publish messages to a topic.
  6. Enter the event loop: The client starts a network loop, which processes incoming and outgoing packets and triggers the appropriate callbacks.
  7. Clean up: When the application is done, it calls a cleanup function to free any resources used by the library.

This architecture provides a flexible and efficient way to build a wide range of messaging applications, from simple IoT sensor readers to more complex systems with real-time data streaming.

Enjoyed this article? Share it with a friend.