go-mqtt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.md +21 -0
- data/NEWS.md +150 -0
- data/README.md +237 -0
- data/lib/mqtt/client.rb +755 -0
- data/lib/mqtt/openssl_fix.rb +29 -0
- data/lib/mqtt/packet.rb +1049 -0
- data/lib/mqtt/proxy.rb +115 -0
- data/lib/mqtt/sn/packet.rb +720 -0
- data/lib/mqtt/version.rb +4 -0
- data/lib/mqtt.rb +48 -0
- data/spec/mqtt_client_spec.rb +1124 -0
- data/spec/mqtt_packet_spec.rb +2012 -0
- data/spec/mqtt_proxy_spec.rb +8 -0
- data/spec/mqtt_sn_packet_spec.rb +1721 -0
- data/spec/mqtt_version_spec.rb +23 -0
- data/spec/zz_client_integration_spec.rb +177 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8698ec3fcf903c009cffb7a8c9f4fefc17704577c05b629e7cf2192c2402de94
|
4
|
+
data.tar.gz: 84deb7a3a57332f01b3d24b86b0dbb1bec8425c5586f851e2fb66aeb4b506b63
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cbcd90f72f086b3ee8e42daf3aaf4138f556932b5240a625dda5229f25e61eb05a3ec5536a8966acb2408ed52240465ef9ec654e93e07a50b41328cccb6aae6a
|
7
|
+
data.tar.gz: a8cae3bc9bdf70ce094332991eb1468acb07b44eb8b0e82c72d3dbadf7bf0ecc8bd490e74a129b74ba497c5ad8c37c76b91fdef24dd76d38c5e8491266854b40
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2009-2013 Nicholas J Humfrey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/NEWS.md
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
Ruby MQTT NEWS
|
2
|
+
==============
|
3
|
+
|
4
|
+
Ruby MQTT Version 0.5.0 (2017-04-16)
|
5
|
+
------------------------------------
|
6
|
+
|
7
|
+
* Switched default protocol version to 3.1.1
|
8
|
+
* Added support for Server Name Identification (SNI)
|
9
|
+
* Fix for unescaping user/password in URI
|
10
|
+
* Fix for bug in MQTT::Proxy class
|
11
|
+
* Add the ability to ignore retained packets when subscribed.
|
12
|
+
* Fix problem of wrong Puback packet ID
|
13
|
+
* Don't keepalive ping if disconnected
|
14
|
+
* Immediately close socket after failed Connack
|
15
|
+
* FakeServer improvements
|
16
|
+
* Fix for working with mathn library.
|
17
|
+
|
18
|
+
|
19
|
+
Ruby MQTT Version 0.4.0 (2016-06-27)
|
20
|
+
------------------------------------
|
21
|
+
|
22
|
+
* Added puback handling for QoS level 1
|
23
|
+
* Low-level MQTT-SN packet parsing support
|
24
|
+
* Allow certs to be set directly instead of just by file
|
25
|
+
* Allow keyphrase for certs to be passed through
|
26
|
+
* Put 'disconnect' inside an 'ensure' block
|
27
|
+
* Fix for error on publish with frozen payload
|
28
|
+
* Fix for packets always getting id 1
|
29
|
+
* Improvements to tests
|
30
|
+
|
31
|
+
|
32
|
+
Ruby MQTT Version 0.3.1 (2014-10-10)
|
33
|
+
------------------------------------
|
34
|
+
|
35
|
+
* Added ```last_ping_response``` to attribute to ```MQTT::Client```
|
36
|
+
|
37
|
+
|
38
|
+
Ruby MQTT Version 0.3.0 (2014-08-26)
|
39
|
+
------------------------------------
|
40
|
+
|
41
|
+
* Added support for MQTT protocol version 3.1.1
|
42
|
+
* Renamed a number of methods/attributes:
|
43
|
+
- Renamed ```:granted_qos``` to ```:return_codes```
|
44
|
+
- Renamed ```:remote_port``` to ```:port```
|
45
|
+
- Renamed ```:remote_host``` to ```:host```
|
46
|
+
- Renamed ```:message_id``` to ```:id```
|
47
|
+
- Renamed ```:protocol_version``` to ```:protocol_level```
|
48
|
+
- Renamed ```MQTT_BROKER``` environment variable to ```MQTT_SERVER```
|
49
|
+
* Added more checks to ensure that the 3.1.1 protocol specs are adhered to
|
50
|
+
* Added a Library Overview section to the README
|
51
|
+
* Added links to the protocol specification to README
|
52
|
+
* Improvements to the YARD API documentation
|
53
|
+
* Don't display payload in inspect if it contains non-visible ASCII characters
|
54
|
+
* Upgraded to rspec 3
|
55
|
+
* Various minor bug fixes and corrections
|
56
|
+
|
57
|
+
|
58
|
+
Ruby MQTT Version 0.2.0 (2014-04-02)
|
59
|
+
------------------------------------
|
60
|
+
|
61
|
+
* Added SSL/TLS support
|
62
|
+
* Added support for passing connection details using a URI
|
63
|
+
* Added support for using the ```MQTT_BROKER``` environment variable
|
64
|
+
* Allow passing array of topics to Client#unsubscribe
|
65
|
+
* Allow more combinations of arguments to be passed to a new Client
|
66
|
+
* No longer defaults to ‘localhost’ if there is no server configured
|
67
|
+
* Fixed more 'unused variable' warnings
|
68
|
+
* Documentation improvements
|
69
|
+
* Ruby 1.8 fixes
|
70
|
+
* Ruby 2 fixes
|
71
|
+
|
72
|
+
|
73
|
+
Ruby MQTT Version 0.1.0 (2013-09-07)
|
74
|
+
------------------------------------
|
75
|
+
|
76
|
+
* Changed license to MIT, to simplify licensing concerns
|
77
|
+
* Improvements for UTF-8 handling under Ruby 1.9
|
78
|
+
* Added ```get_packet``` method
|
79
|
+
* Added support for a keep-alive value of 0
|
80
|
+
* Added a #inspect method to the Packet classes
|
81
|
+
* Added checks for the protocol name and version
|
82
|
+
* Added check to ensure that packet body isn't too big
|
83
|
+
* Added validation of QoS value
|
84
|
+
* Added example of using authentication
|
85
|
+
* Fixed 'unused variable' warnings
|
86
|
+
* Reduced duplicated code in packet parsing
|
87
|
+
* Improved testing
|
88
|
+
- Created fake server and integration tests
|
89
|
+
- Better test coverage
|
90
|
+
- Added more tests for error states
|
91
|
+
|
92
|
+
|
93
|
+
Ruby MQTT Version 0.0.9 (2012-12-21)
|
94
|
+
------------------------------------
|
95
|
+
|
96
|
+
* Fixes for Ruby 1.9.3 by Mike English
|
97
|
+
* Fix for ```client_id``` typo by Anubisss
|
98
|
+
* Added methods to inspect the incoming message queue: ```queue_empty?``` and ```queue_length```
|
99
|
+
* Fixed incorrect implementation of the parsing and serialising of Subscription Acknowledgement packets
|
100
|
+
* Changed test mocking from Mocha to rspec-mocks
|
101
|
+
|
102
|
+
|
103
|
+
Ruby MQTT Version 0.0.8 (2011-02-04)
|
104
|
+
------------------------------------
|
105
|
+
|
106
|
+
* Implemented Last Will and Testament feature
|
107
|
+
* Renamed dup attribute to duplicate to avoid method name clash
|
108
|
+
* Made the random ```client_id``` generator a public class method
|
109
|
+
|
110
|
+
|
111
|
+
Ruby MQTT Version 0.0.7 (2011-01-19)
|
112
|
+
------------------------------------
|
113
|
+
|
114
|
+
* You can now pass a topic and block to client.get
|
115
|
+
* Added MQTT::Client.connect class method
|
116
|
+
|
117
|
+
|
118
|
+
Ruby MQTT Version 0.0.5 (2011-01-18)
|
119
|
+
------------------------------------
|
120
|
+
|
121
|
+
* Implemented setting username and password (MQTT 3.1)
|
122
|
+
* Renamed ```clean_start``` to ``clean_session```
|
123
|
+
* Started using autoload to load classes
|
124
|
+
* Modernised Gem building mechanisms
|
125
|
+
|
126
|
+
|
127
|
+
Ruby MQTT Version 0.0.4 (2009-02-22)
|
128
|
+
------------------------------------
|
129
|
+
|
130
|
+
* Re-factored packet encoding/decoding into one class per packet type
|
131
|
+
* Added MQTT::Proxy class for implementing an MQTT proxy
|
132
|
+
|
133
|
+
|
134
|
+
Ruby MQTT Version 0.0.3 (2009-02-08)
|
135
|
+
------------------------------------
|
136
|
+
|
137
|
+
* Added checking of Connection Acknowledgement
|
138
|
+
* Automatic client identifier generation
|
139
|
+
|
140
|
+
|
141
|
+
Ruby MQTT Version 0.0.2 (2009-02-03)
|
142
|
+
------------------------------------
|
143
|
+
|
144
|
+
* Added support for packets longer than 127 bytes
|
145
|
+
|
146
|
+
|
147
|
+
Ruby MQTT Version 0.0.1 (2009-02-01)
|
148
|
+
------------------------------------
|
149
|
+
|
150
|
+
* Initial Release
|
data/README.md
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
[](https://github.com/njh/ruby-mqtt/actions/workflows/build.yml?query=branch%3Amain)
|
2
|
+
|
3
|
+
ruby-mqtt
|
4
|
+
=========
|
5
|
+
|
6
|
+
Pure Ruby gem that implements the [MQTT] protocol, a lightweight protocol for publish/subscribe messaging.
|
7
|
+
|
8
|
+
Also includes a class for parsing and generating [MQTT-SN] packets.
|
9
|
+
|
10
|
+
|
11
|
+
Table of Contents
|
12
|
+
-----------------
|
13
|
+
* [Installation](#installation)
|
14
|
+
* [Quick Start](#quick-start)
|
15
|
+
* [Library Overview](#library-overview)
|
16
|
+
* [Resources](#resources)
|
17
|
+
* [License](#license)
|
18
|
+
* [Contact](#contact)
|
19
|
+
|
20
|
+
|
21
|
+
Installation
|
22
|
+
------------
|
23
|
+
|
24
|
+
You may get the latest stable version from [Rubygems]:
|
25
|
+
|
26
|
+
$ gem install mqtt
|
27
|
+
|
28
|
+
Alternatively, to use a development snapshot from GitHub using [Bundler], add this to your `Gemfile`:
|
29
|
+
|
30
|
+
~~~ ruby
|
31
|
+
gem 'mqtt', :github => 'njh/ruby-mqtt'
|
32
|
+
~~~
|
33
|
+
|
34
|
+
Quick Start
|
35
|
+
-----------
|
36
|
+
|
37
|
+
~~~ ruby
|
38
|
+
require 'mqtt'
|
39
|
+
|
40
|
+
# Publish example
|
41
|
+
MQTT::Client.connect('test.mosquitto.org') do |c|
|
42
|
+
c.publish('test', 'message')
|
43
|
+
end
|
44
|
+
|
45
|
+
# Subscribe example
|
46
|
+
MQTT::Client.connect('test.mosquitto.org') do |c|
|
47
|
+
# If you pass a block to the get method, then it will loop
|
48
|
+
c.get('test') do |topic,message|
|
49
|
+
puts "#{topic}: #{message}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
~~~
|
53
|
+
|
54
|
+
|
55
|
+
Library Overview
|
56
|
+
----------------
|
57
|
+
|
58
|
+
### Connecting ###
|
59
|
+
|
60
|
+
A new client connection can be created by passing either a [MQTT URI], a host and port or by passing a hash of attributes.
|
61
|
+
|
62
|
+
~~~ ruby
|
63
|
+
client = MQTT::Client.connect('mqtt://myserver.example.com')
|
64
|
+
client = MQTT::Client.connect('mqtts://user:pass@myserver.example.com')
|
65
|
+
client = MQTT::Client.connect('myserver.example.com')
|
66
|
+
client = MQTT::Client.connect('myserver.example.com', 18830)
|
67
|
+
client = MQTT::Client.connect(:host => 'myserver.example.com', :port => 1883 ... )
|
68
|
+
~~~
|
69
|
+
|
70
|
+
TLS/SSL is not enabled by default, to enabled it, pass ```:ssl => true```:
|
71
|
+
|
72
|
+
~~~ ruby
|
73
|
+
client = MQTT::Client.connect(
|
74
|
+
:host => 'test.mosquitto.org',
|
75
|
+
:port => 8883,
|
76
|
+
:ssl => true
|
77
|
+
)
|
78
|
+
~~~
|
79
|
+
|
80
|
+
Alternatively you can create a new Client object and then configure it by setting attributes. This example shows setting up client certificate based authentication:
|
81
|
+
|
82
|
+
~~~ ruby
|
83
|
+
client = MQTT::Client.new
|
84
|
+
client.host = 'myserver.example.com'
|
85
|
+
client.ssl = true
|
86
|
+
client.cert_file = path_to('client.pem')
|
87
|
+
client.key_file = path_to('client.key')
|
88
|
+
client.ca_file = path_to('root-ca.pem')
|
89
|
+
client.connect()
|
90
|
+
~~~
|
91
|
+
The default timeout when opening a TCP Socket is 90 seconds. To specify it explicitly, use 'connect_timeout =>':
|
92
|
+
|
93
|
+
~~~ ruby
|
94
|
+
client = MQTT::Client.connect(
|
95
|
+
:host => 'myserver.example.com',
|
96
|
+
:connect_timeout => 15
|
97
|
+
)
|
98
|
+
~~~
|
99
|
+
|
100
|
+
The connection can either be made without the use of a block:
|
101
|
+
|
102
|
+
~~~ ruby
|
103
|
+
client = MQTT::Client.connect('test.mosquitto.org')
|
104
|
+
# perform operations
|
105
|
+
client.disconnect()
|
106
|
+
~~~
|
107
|
+
|
108
|
+
Or, if using a block, with an implicit disconnection at the end of the block.
|
109
|
+
|
110
|
+
~~~ ruby
|
111
|
+
MQTT::Client.connect('test.mosquitto.org') do |client|
|
112
|
+
# perform operations
|
113
|
+
end
|
114
|
+
~~~
|
115
|
+
|
116
|
+
For more information, see and list of attributes for the [MQTT::Client] class and the [MQTT::Client.connect] method.
|
117
|
+
|
118
|
+
|
119
|
+
### Publishing ###
|
120
|
+
|
121
|
+
To send a message to a topic, use the ```publish``` method:
|
122
|
+
|
123
|
+
~~~ ruby
|
124
|
+
client.publish(topic, payload, retain=false)
|
125
|
+
~~~
|
126
|
+
|
127
|
+
The method will return once the message has been sent to the MQTT server.
|
128
|
+
|
129
|
+
For more information see the [MQTT::Client#publish] method.
|
130
|
+
|
131
|
+
|
132
|
+
### Subscribing ###
|
133
|
+
|
134
|
+
You can send a subscription request to the MQTT server using the subscribe method. One or more [Topic Filters] may be passed in:
|
135
|
+
|
136
|
+
~~~ ruby
|
137
|
+
client.subscribe( 'topic1' )
|
138
|
+
client.subscribe( 'topic1', 'topic2' )
|
139
|
+
client.subscribe( 'foo/#' )
|
140
|
+
~~~
|
141
|
+
|
142
|
+
For more information see the [MQTT::Client#subscribe] method.
|
143
|
+
|
144
|
+
|
145
|
+
### Receiving Messages ###
|
146
|
+
|
147
|
+
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:
|
148
|
+
|
149
|
+
~~~ ruby
|
150
|
+
topic,message = client.get
|
151
|
+
~~~
|
152
|
+
|
153
|
+
Alternatively, you can give the get method a block, which will be called for every message received and loop forever:
|
154
|
+
|
155
|
+
~~~ ruby
|
156
|
+
client.get do |topic,message|
|
157
|
+
# Block is executed for every message received
|
158
|
+
end
|
159
|
+
~~~
|
160
|
+
|
161
|
+
For more information see the [MQTT::Client#get] method.
|
162
|
+
|
163
|
+
|
164
|
+
### Parsing and serialising of packets ###
|
165
|
+
|
166
|
+
The parsing and serialising of MQTT and MQTT-SN packets is a separate lower-level API.
|
167
|
+
You can use it to build your own clients and servers, without using any of the rest of the
|
168
|
+
code in this gem.
|
169
|
+
|
170
|
+
~~~ ruby
|
171
|
+
# Parse a string containing a binary packet into an object
|
172
|
+
packet_obj = MQTT::Packet.parse(binary_packet)
|
173
|
+
|
174
|
+
# Write a PUBACK packet to an IO handle
|
175
|
+
ios << MQTT::Packet::Puback(:id => 20)
|
176
|
+
|
177
|
+
# Write an MQTT-SN Publish packet with QoS -1 to a UDP socket
|
178
|
+
socket = UDPSocket.new
|
179
|
+
socket.connect('localhost', MQTT::SN::DEFAULT_PORT)
|
180
|
+
socket << MQTT::SN::Packet::Publish.new(
|
181
|
+
:topic_id => 'TT',
|
182
|
+
:topic_id_type => :short,
|
183
|
+
:data => "The time is: #{Time.now}",
|
184
|
+
:qos => -1
|
185
|
+
)
|
186
|
+
socket.close
|
187
|
+
~~~
|
188
|
+
|
189
|
+
Limitations
|
190
|
+
-----------
|
191
|
+
|
192
|
+
* QoS 2 is not currently supported by client
|
193
|
+
* Automatic re-connects to the server are not supported
|
194
|
+
* No local persistence for packets
|
195
|
+
|
196
|
+
|
197
|
+
Resources
|
198
|
+
---------
|
199
|
+
|
200
|
+
* API Documentation: http://rubydoc.info/gems/mqtt
|
201
|
+
* Protocol Specification v3.1.1: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
|
202
|
+
* Protocol Specification v3.1: http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
|
203
|
+
* MQTT-SN Protocol Specification v1.2: http://mqtt.org/new/wp-content/uploads/2009/06/MQTT-SN_spec_v1.2.pdf
|
204
|
+
* MQTT Homepage: http://www.mqtt.org/
|
205
|
+
* GitHub Project: http://github.com/njh/ruby-mqtt
|
206
|
+
|
207
|
+
|
208
|
+
License
|
209
|
+
-------
|
210
|
+
|
211
|
+
The mqtt ruby gem is licensed under the terms of the MIT license.
|
212
|
+
See the file LICENSE for details.
|
213
|
+
|
214
|
+
|
215
|
+
Contact
|
216
|
+
-------
|
217
|
+
|
218
|
+
* Author: Nicholas J Humfrey
|
219
|
+
* Email: njh@aelius.com
|
220
|
+
* Twitter: [@njh]
|
221
|
+
* Home Page: http://www.aelius.com/njh/
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
[@njh]: http://twitter.com/njh
|
226
|
+
[MQTT]: http://www.mqtt.org/
|
227
|
+
[MQTT-SN]: http://mqtt.org/2013/12/mqtt-for-sensor-networks-mqtt-sn
|
228
|
+
[Rubygems]: http://rubygems.org/
|
229
|
+
[Bundler]: http://bundler.io/
|
230
|
+
[MQTT URI]: https://github.com/mqtt/mqtt.github.io/wiki/URI-Scheme
|
231
|
+
[Topic Filters]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html#_Toc388534397
|
232
|
+
|
233
|
+
[MQTT::Client]: http://rubydoc.info/gems/mqtt/MQTT/Client#instance_attr_details
|
234
|
+
[MQTT::Client.connect]: http://rubydoc.info/gems/mqtt/MQTT/Client.connect
|
235
|
+
[MQTT::Client#publish]: http://rubydoc.info/gems/mqtt/MQTT/Client:publish
|
236
|
+
[MQTT::Client#subscribe]: http://rubydoc.info/gems/mqtt/MQTT/Client:subscribe
|
237
|
+
[MQTT::Client#get]: http://rubydoc.info/gems/mqtt/MQTT/Client:get
|