Project

mosca

0.01
No commit activity in last 3 years
No release in over 3 years
A simple client for mqtt communication
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0

Runtime

~> 1.8.1
>= 0.3.1, ~> 0.3
 Project Readme

Version     Dependency Status Code Climate Build Status Coverage

Mosca

A MQTT library wrapper for ruby. Currently uses ruby-mqtt for connection and packet handling.

Features

  • Automatically handles connection / reconnection when trying to get or publish messages. Good for background processes that deal with a high load of messages and need reliability and single connection handling.
  • Allows specifying timeout on all of the operations.
  • Simplifies 2-way communication by setting an out topic and an in topic to automatically wait for responses.
  • Keeps only 1 connection open with the broker.

Installation

Add this line to your application's Gemfile:

gem 'mosca'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mosca

Usage

Configure

You can configure the default timeout for response, and default mqtt broker.

  Mosca::Client.default_timeout = 10 # 10 seconds
  Mosca::Client.default_broker = "test.mosquitto.org"

Environment variables

The following environment variables are used when creating clients, so you can use something like dotenv to put them on a file:

MOSCA_BROKER
MOSCA_USER
MOSCA_PASS
MOSCA_TIMEOUT

New instance

  # Params (all optional)
  # user
  # pass
  # broker
  # topic_in
  # topic_out
  # topic_base (topics out and in are appended to this)

  client = Mosca::Client.new user: "username", pass: "password", topic_in: "readings", topic_out: "commands", topic_base: "/device/"

Publishing

Single message

  client.publish "restart" # will be sent to topic /device/commands. Returns nil if timed out.
  client.publish! "restart" # Raises Timeout::Error if timed out.

Message with response

  response = client.publish "some_command", response: true, topic_in: "responses" # will publish and wait for a response on the /device/responses topic
  

Getting messages

  puts client.get # will wait up to Mosca.default_timeout (default 5) seconds. will return nil if no response comes.
  puts client.get timeout: 2, topic_in: "another_topic" # will wait up to 2 seconds for a response on the another_topic topic.
  client.get! # Raises exception if timed out

###TO DO

Readme not complete yet

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request