Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

mosquitto - a high perf MQTT 3.1 client

Build Status

The mosquitto gem is meant to serve as an easy, performant and standards compliant client for interacting with MQTT brokers.

The API consists of two classes:

Mosquitto::Client - the client

Mosquitto::Message - returned from the client

About MQTT and libmosquitto

MQ Telemetry Transport is :

MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging
protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design
principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure
reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of
the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile
applications where bandwidth and battery power are at a premium.

Please see the FAQ and list of supported software.


Mosquitto is an open source (BSD licensed) message broker that implements the MQ Telemetry Transport protocol
version 3.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This
makes it suitable for "machine to machine" messaging such as with low power sensors or mobile devices such as
phones, embedded computers or microcontrollers like the Arduino. 

See the project website for more information.


This gem links against version 1.3.1 of libmosquitto . You will need to install additional packages for your system.


The preferred installation method for libmosquitto on OS X is with the Homebrew package manager :

brew install mosquitto

Linux Ubuntu

sudo apt-get update
sudo apt-get install pkg-config cmake openssl libc-ares-dev

The current Ubuntu packages aren't on 1.3.1 yet - it's recommended to build libmosquitto from source (see below) until further notice. OpenSSL is an optional dependency - libmosquitto builds without it, however TLS specific features would not be available.

Building libmosquitto from source

tar xzf mosquitto-1.3.1.tar.gz
cd mosquitto-1.3.1
cmake .
sudo make install


This gem is regularly tested against the following Ruby versions on Linux and Mac OS X:

  • Ruby MRI 1.9.3
  • Ruby MRI 2.0.0 (ongoing patch releases).
  • Ruby MRI 2.1.0 (ongoing patch releases).
  • Ruby MRI 2.1.1 (ongoing patch releases).

Ruby 1.8, Rubinius and JRuby are not supported.


OSX / Linux

When are requirements or dependencies are met, the following should install mosquitto without any problems :

gem install mosquitto

The extension checks for libmosquitto presence as well as a 1.3.1 version.


Threaded loop

The simplest mode of operation - starts a new thread to process network traffic.

require 'mosquitto'

publisher ="blocking")

# Spawn a network thread with a main loop

# On publish callback
publisher.on_publish do |mid|
  p "Published #{mid}"

# On connect callback
publisher.on_connect do |rc|
  p "Connected with return code #{rc}"
  # publish a test message once connected
  publisher.publish(nil, "topic", "test message", Mosquitto::AT_MOST_ONCE, true)

# Connect to MQTT broker
publisher.connect("", 1883, 10)

# Allow some time for processing
sleep 2


# Optional, stop the threaded loop - the network thread would be reaped on Ruby exit as well

Blocking loop (simple clients)

The client supports a blocking main loop as well which is useful for building simple MQTT clients. Reconnects etc. and other misc operations are handled for you.

require 'mosquitto'

publisher ="blocking")

# On publish callback
publisher.on_publish do |mid|
  p "Published #{mid}"

# On connect callback
publisher.on_connect do |rc|
  p "Connected with return code #{rc}"
  # publish a test message once connected
  publisher.publish(nil, "topic", "test message", Mosquitto::AT_MOST_ONCE, true)

# Connect to MQTT broker
publisher.connect("", 1883, 10)

# Blocking main loop

Custom main loop

EventMachine support is forthcoming.


The client supports any of the Ruby Logger libraries with the standard #add method interface

require 'mosquitto'

publisher ="blocking")

# Sets a custom log callback for us that pipes to the given logger instance
publisher.logger =

# Connect to MQTT broker
publisher.connect("", 1883, 10)


The following callbacks are supported (please follow links for further documentation) :

  • connect - called when the broker sends a CONNACK message in response to a connection.
  • disconnect - called when the broker has received the DISCONNECT command and has disconnected the client.
  • log - should be used if you want event logging information from the client library.
  • subscribe - called when the broker responds to a subscription request.
  • unsubscribe - called when the broker responds to a unsubscription request.
  • publish - called when a message initiated with Mosquitto::Client#publish has been sent to the broker successfully.
  • message - called when a message is received from the broker.


libmosquitto builds with TLS support by default, however pre-shared key (PSK) support is not available when linked against older OpenSSL versions.

tls_client =

tls_client.logger =


tls_client.on_connect do |rc|
  p :tls_connection
tls_client.tls_opts_set(Mosquitto::SSL_VERIFY_PEER, "tlsv1.2", nil)
tls_client.tls_set('/path/to/'), nil, nil, nil, nil)
tls_client.connect('', 8883, 10)

See documentation for the full API specification.

Contact, feedback and bugs

This extension is currently maintained by Lourens Naudé ( and contributors.

Please file bugs / issues and feature requests on the issue tracker


To run the tests, you can use RVM and Bundler to create a pristine environment for mosquitto development/hacking. Use 'bundle install' to install the necessary development and testing gems:

bundle install
bundle exec rake

Tests by default run against an MQTT server on localhost, which is expected to support TLS on port 8883 as well.

class MosquittoTestCase < Test::Unit::TestCase
  TEST_HOST = "localhost"
  TEST_PORT = 1883

  TLS_TEST_HOST = "localhost"
  TLS_TEST_PORT = 8883


Documentation available at

Special Thanks

  • Roger Light - for libmosquitto


Ruby binding against libmosquitto ( - a high performance MQTT protocol ( client







No packages published

Contributors 4