• Stars
    star
    512
  • Rank 86,323 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.

Build Status

ruby-mqtt

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.

Also includes a class for parsing and generating MQTT-SN packets.

Table of Contents

Installation

You may get the latest stable version from Rubygems:

$ gem install mqtt

Alternatively, to use a development snapshot from GitHub using Bundler, add this to your Gemfile:

    gem 'mqtt', :github => 'njh/ruby-mqtt'

Quick Start

require 'mqtt'

# Publish example
MQTT::Client.connect('test.mosquitto.org') do |c|
  c.publish('test', 'message')
end

# Subscribe example
MQTT::Client.connect('test.mosquitto.org') do |c|
  # If you pass a block to the get method, then it will loop
  c.get('test') do |topic,message|
    puts "#{topic}: #{message}"
  end
end

Library Overview

Connecting

A new client connection can be created by passing either a MQTT URI, a host and port or by passing a hash of attributes.

client = MQTT::Client.connect('mqtt://myserver.example.com')
client = MQTT::Client.connect('mqtts://user:[email protected]')
client = MQTT::Client.connect('myserver.example.com')
client = MQTT::Client.connect('myserver.example.com', 18830)
client = MQTT::Client.connect(:host => 'myserver.example.com', :port => 1883 ... )

TLS/SSL is not enabled by default, to enabled it, pass :ssl => true:

client = MQTT::Client.connect(
  :host => 'test.mosquitto.org',
  :port => 8883,
  :ssl => true
)

Alternatively you can create a new Client object and then configure it by setting attributes. This example shows setting up client certificate based authentication:

client = MQTT::Client.new
client.host = 'myserver.example.com'
client.ssl = true
client.cert_file = path_to('client.pem')
client.key_file  = path_to('client.key')
client.ca_file   = path_to('root-ca.pem')
client.connect()

The connection can either be made without the use of a block:

client = MQTT::Client.connect('test.mosquitto.org')
# perform operations
client.disconnect()

Or, if using a block, with an implicit disconnection at the end of the block.

MQTT::Client.connect('test.mosquitto.org') do |client|
  # perform operations
end

For more information, see and list of attributes for the MQTT::Client class and the MQTT::Client.connect method.

Publishing

To send a message to a topic, use the publish method:

client.publish(topic, payload, retain=false)

The method will return once the message has been sent to the MQTT server.

For more information see the MQTT::Client#publish method.

Subscribing

You can send a subscription request to the MQTT server using the subscribe method. One or more Topic Filters may be passed in:

client.subscribe( 'topic1' )
client.subscribe( 'topic1', 'topic2' )
client.subscribe( 'foo/#' )

For more information see the MQTT::Client#subscribe method.

Receiving Messages

To receive a message, use the get method. This method will block until a message is available. The topic is the name of the topic the message was sent to. The message is a string:

topic,message = client.get

Alternatively, you can give the get method a block, which will be called for every message received and loop forever:

client.get do |topic,message|
  # Block is executed for every message received
end

For more information see the MQTT::Client#get method.

Parsing and serialising of packets

The parsing and serialising of MQTT and MQTT-SN packets is a separate lower-level API. You can use it to build your own clients and servers, without using any of the rest of the code in this gem.

# Parse a string containing a binary packet into an object
packet_obj = MQTT::Packet.parse(binary_packet)
    
# Write a PUBACK packet to an IO handle
ios << MQTT::Packet::Puback(:id => 20)
    
# Write an MQTT-SN Publish packet with QoS -1 to a UDP socket
socket = UDPSocket.new
socket.connect('localhost', MQTT::SN::DEFAULT_PORT)
socket << MQTT::SN::Packet::Publish.new(
  :topic_id => 'TT',
  :topic_id_type => :short,
  :data => "The time is: #{Time.now}",
  :qos => -1
)
socket.close

Limitations

  • QoS 2 is not currently supported by client
  • Automatic re-connects to the server are not supported
  • No local persistence for packets

Resources

License

The mqtt ruby gem is licensed under the terms of the MIT license. See the file LICENSE for details.

Contact

More Repositories

1

EtherCard

EtherCard is an IPv4 driver for the ENC28J60 chip, compatible with Arduino IDE
C++
994
star
2

mqtt-sn-tools

Command line tools written in C for the MQTT-SN (MQTT for Sensor Networks) protocol
C
189
star
3

marquette

MQTT enabled Control panel written in node.js
JavaScript
156
star
4

sonoff-ota-flash-cli

Bash script to perform an OTA (Over the Air) firmware update for Sonoff 'DIY Mode' device on the command line
Shell
135
star
5

mqtt-http-bridge

MQTT to HTTP bridge
Ruby
115
star
6

mast

MAST (Multicast Audio Streaming Toolkit) is set of audio streaming tools using RTP over IPv4 and IPv6 Multicast/Unicast.
C
96
star
7

NanodeMQTT

MQTT for Nanode
C++
67
star
8

EtherSia

Minimal IPv6 library for Ethernet controllers on Arduino
C++
59
star
9

redstore

RedStore is a lightweight RDF triplestore written in C using the Redland library.
C
58
star
10

arduino-libraries

A website listing all the available Arduino Libraries
Ruby
52
star
11

twolame

MPEG Audio Layer 2 (MP2) encoder
C
51
star
12

rotter

Rotter is a Recording of Transmission / Audio Logger for JACK.
C
47
star
13

jackmeter

Jack Meter is a basic console based DPM (Digital Peak Meter) for JACK.
C
42
star
14

ruby-em-mqtt

MQTT support for EventMachine
Ruby
40
star
15

perl-net-sdp

Perl Module : Session Description Protocol (rfc2327)
Perl
36
star
16

dbpedialite

Sinatra project to produce a slimmed down dbpedia with stable identifiers.
Ruby
36
star
17

jackminimix

JackMiniMix is a simple mixer for the Jack Audio Connection Kit with an OSC based control interface.
C
27
star
18

W5500MacRaw

Arduino sketch demonstrating using Wiznet W5500 to send raw Ethernet Frames
C++
26
star
19

silentjack

SilentJack is a silence/dead air detector for the Jack Audio Connection Kit.
C
22
star
20

node-red-contrib-osc

Open Sound Control (OSC) support for Node-RED
JavaScript
21
star
21

binmark

Markup language and tool for generating binary files
C
21
star
22

gen-webid-cert

Shell script to generate a Self-signed Certificate for use with WebID
Shell
14
star
23

twig-html-helpers

Rails style view helpers for HTML tags in PHP Twig
PHP
13
star
24

rdate

rdate connects to an RFC 868 time server over a TCP/IP network, printing the returned time and/or setting the system clock.
C
12
star
25

eagle-library

12
star
26

node-red-schedule

Display a schedule for all your node-red inject nodes
JavaScript
11
star
27

ruby-em-mqtt-sn

MQTT-SN protocol and gateway support for EventMachine
Ruby
10
star
28

google-ipv6

Per-country ranking table based on data from Google IPv6 Statistics page
HTML
10
star
29

dymo-root-ca-security-risk

An analysis of the DYMO Root CA Security Risk
10
star
30

node-owfs

A node.js client library for the owserver protocol in owfs
JavaScript
10
star
31

radiodns-uk

A directory of RadioDNS enabled radio stations in the UK
Ruby
10
star
32

dvbshout

dvbshout takes an MPEG transport stream from a DVB card, extracts audio channels from stream, and sends the audio to an Icecast / Shoutcast server and/or a RTP multicast packet stream.
C
9
star
33

node-red-contrib-pcap

Network packet capture for Node-RED
HTML
8
star
34

node-red-contrib-owfs

Node-RED node for talking to 1-wire devices using owfs
HTML
8
star
35

madjack

MadJACK is a MPEG Audio Deck for the Jack Audio Connection Kit with an OSC based control interface.
C
8
star
36

ruby-waveinfo

waveinfo is a pure-ruby gem to get the information from the headers of Wave (.wav) files.
Ruby
8
star
37

perl-net-rtp

Perl Module : Send and receive RTP packets (RFC3550)
Perl
7
star
38

W5100MacRaw

Arduino sketch demonstrating using Wiznet W5100 to send raw Ethernet Frames
C++
7
star
39

njh.me

PHP web application that I use for my personal WebId
PHP
7
star
40

ruby-playlist

A ruby gem to convert playlists between different formats
Ruby
7
star
41

mp3-to-hls

Ruby script to convert an MP3 file into an HLS stream
Ruby
6
star
42

twitter-stream-to-mqtt

Listen to Twitter Steaming API and publish to MQTT
Ruby
6
star
43

ruby-bbc-programmes

BBC Programmes Library for Ruby
Ruby
6
star
44

node-red-contrib-collector

HTML
6
star
45

owmqtt

1-wire to MQTT Bridge
C
6
star
46

wavemetatools

A collection of tools to manipulate WAVE files, with an emphasis on the metadata used by the radio industry.
C
6
star
47

vyos-firewall-generator

Dual-stack VyOS Zone based Firewall Generator
Ruby
5
star
48

perl-musicbrainz-discid

Perl Module : Perl interface for the MusicBrainz libdiscid library
Perl
5
star
49

radiovis-to-mqtt

Script to relay RadioVis messages to MQTT
Ruby
5
star
50

mast-ctl

Control Panel for the Multicast Audio Streaming Toolkit
JavaScript
5
star
51

captivednsd

captivednsd, the Captive Domain Name Server, returns same authorative response to every query.
C
5
star
52

ts2mpa

ts2mpa is a simple tool to extract MPEG Audio from a MPEG-2 Transport Stream.
C
4
star
53

k8055mqtt

Self contained MQTT client for controlling the the Velleman K8055 / VM110 Board
C
3
star
54

DangerMinusOne

Send MQTT-SN QoS -1 messages from a Arduino Danger Shield
C++
3
star
55

delphyne

Mythic Beasts DNS API client implemented as a bash script
Shell
3
star
56

node-mqtt-sse-example

Example of using node.js, Server Sent Events and MQTT together
JavaScript
3
star
57

lunchboxradio

Lunchbox Radio is a project to help people build their own IP radio out of off-the-shelf hardware and software.
Shell
3
star
58

k8055httpd

Self contained HTTP server for controlling the the Velleman K8055 / VM110 Board
C
3
star
59

perl-websphere-mqtt-client

Perl Module : WebSphere MQ Telemetry Transport Client
C
2
star
60

dennet

Home Automation Experiment
2
star
61

ruby-esgob

Command line tool and library for talking to the Esgob anycast DNS API
Ruby
2
star
62

perl-net-ortp

Perl Module : Perl interface to the oRTP C library
Perl
2
star
63

nanode-flash-led

Pure-C program to flash the red LED on the Nanode
Makefile
2
star
64

relay-driver-shield

An Arduino shield for controlling four 12v relays
2
star
65

ruby-sqlitecache

SQLiteCache is a gem to allow you cache slow queries in ruby code. It provides a simple API to make it easy to add caching to your ruby code.
Ruby
2
star
66

bbc-programmes-php

PHP Library for accessing data on bbc.co.uk/programmes
PHP
2
star
67

perl-net-sap

Perl Module : Session Announcement Protocol (rfc2974)
Perl
2
star
68

primateplunge

Help poor Monkey navigate his way down through trecherous jungles, canyons, towering waterfalls, into the heart of a volcano and through the driving rain of a cold industrial city.
C
2
star
69

ruby-matrixorbital-glk

MatrixOrbital-GLK is a ruby gem for controlling the GLK series of LCD screens made by Matrix Orbital.
Ruby
2
star
70

node-red-contrib-wireshark

Capture and decode packets from a network interface using Wireshark
HTML
1
star
71

node-red-contrib-slip

Encoding and decoding for SLIP packets for Node-RED
HTML
1
star
72

osc2k8056

OSC server to send commands to the Velleman K8056 kit
C
1
star
73

bbcradio-tingapp

Listen to BBC Radio on your Tingbot
Python
1
star
74

velux-blind-control

Velux Blind Control Arduino Sketch by hacking a Velux remote control
C++
1
star
75

ihlc

Industruino Home Lighting Controller
Arduino
1
star
76

avrsh

AVR command shell
C++
1
star
77

perl-io-socket-multicast6

Perl Module : Send and receive IPv4 and IPv6 multicast messages
Perl
1
star
78

osc2k8062

OSC to DMX bridge server for the Velleman K8062 kit
C
1
star
79

tingbot-heating

Heating controller for my house, using Tingbot
Python
1
star
80

perl-socket-multicast6

Perl Module : Constructors and constants for IPv4 and IPv6 multicast socket operations.
Perl
1
star
81

perl-device-quasar3108

Perl Module : Control Quasar Electronics Serial I/O Module
Perl
1
star
82

perl-net-dvbstreamer

Perl Module : Control DVBStreamer from perl
Perl
1
star
83

echolife-hg612-dslstats

Ruby script to get DSL connection rate stats from an EchoLife HG612 modem
Ruby
1
star
84

perl-liblo

Perl interface for liblo Lightweight OSC library
Perl
1
star
85

SevenSeg

Arduino Library to make easy to control the SparkFun 7-Segment Serial Display
C++
1
star
86

mqtt-callerid

BT CallerID to MQTT bridge for POSIX systems
C
1
star
87

travis-ipv6-test

Tests to see if Travis supports IPv6
Ruby
1
star
88

perl-device-matrixorbital-glk

Perl Module - Control the GLK series Matrix Orbital displays
Perl
1
star
89

ruby-dvbstreamer

The DVBStreamer Gem provides a control interface to DVBStreamer servers. Use it to perform any of the commands supported by DVBStreamer's remote control interface.
Ruby
1
star
90

tinyppp6

Tiny implementation of PPP for IPv6
C
1
star
91

node-red-contrib-mapper

A Node-RED node that maps the contents of a message property from one value to another.
HTML
1
star
92

node-heatmiser-modbus

node.js library for controlling Heatmiser Modbus Thermostats
TypeScript
1
star
93

Doorbell

Arduino project for my home doorbell
C++
1
star
94

ruby-mqtt-packet

MQTT and MQTT-SN packet parser and generator
Ruby
1
star
95

ruby-mpris

MPRIS is the Media Player Remote Interfacing Specification. This ruby gem allows you to control media players that implement the MPRIS specification over D-Bus.
Ruby
1
star