You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 28, 2022. It is now read-only.
- adds `text`, `json`, `binary`, and `base64` encoders and decoders to
be used with `publish()` and `subscribe()`, respectively
- can set defaults with options to `connect()`
- the default is `text` for both encoder and decoder
- update docs, tests
- `unsubscribe` returns `true` if actual unsubscription occurs; `false`
otherwise
There are two (2) main issues which users of MQTT.js will quickly encounter:
31
31
32
32
1. You can't just set a listener function for a subscribed topic; you have to stuff logic into a listener on the `message` event, and figure out what to do with the topic.
33
-
2. Received messages are all `Buffer`s, and when publishing, your message must be a `string`, `Buffer`.
33
+
2. Received messages are all `Buffer` objects, and you can only publish a `Buffer` or `string`.
34
34
3. (BONUS ISSUE) It doesn't use `Promise`s, which some people prefer over callbacks.
35
35
36
-
`mqttletoad` solves the *first* problem and the *third* problem.
36
+
`mqttletoad` solves the above problems. These are problems I have, and likely many others have as well.
37
37
38
38
### Listeners for Specific Topics
39
39
@@ -57,42 +57,96 @@ We need something like a *router* (think [express](https://www.npmjs.com/package
57
57
58
58
What's better is that `EventEmitter`s are standardized. They are easy to consume. Think [RxJs](https://npm.im/rxjs)'s `Observable.fromEvent()`. This should help those using a "reactive" programming model.
59
59
60
-
### Promise Support
60
+
### Encoding and Decoding
61
61
62
-
[async-mqtt](https://npm.im/async-mqtt) does the same thing here--more or less.
62
+
MQTT makes no prescriptions about what a message looks like. It's just a [blob](https://en.wikipedia.org/wiki/Binary_large_object).
63
+
64
+
But as a developer, you know your data. Maybe that message is JSON, maybe it's base64-encoded, or maybe it's just a string. You are unlikely to be surprised about what you get.
65
+
66
+
Out-of-the-box, `mqttletoad` provides several common *decoders* for received messages, and *encoders* for publishing messages.
67
+
68
+
These are:
63
69
64
-
### WONTFIX: Message Formats
70
+
-`json` - Convert to/from a JSON representation of an object
71
+
-`text` - Convert to/from a UTF-8 encoded string
72
+
-`binary` - Convert to a `Buffer` (all received messages are `Buffer`s, so no decoding necessary)
73
+
-`base64` - Convert to/from a base64 (string) representation of just about anything
65
74
66
-
MQTT makes no prescriptions about what your message looks like. It's just a [blob](https://en.wikipedia.org/wiki/Binary_large_object). A JavaScript object is neither of these! If you need to publish an object, call `JSON.stringify` on it first:
75
+
To use these, you can specify a *default* encoder and/or decoder when connecting:
// remove ALL listeners from this topic and unsubscribe
200
+
awaitclient.unsubscribe('winken/+/nod');
201
+
202
+
// disconnect
203
+
awaitclient.end();
127
204
}
128
205
```
129
206
130
-
## API
131
-
132
-
Basically it's [async-mqtt](https://npm.im/async-mqtt) (which is [mqtt](https://npm.im/mqtt)) except:
133
-
134
207
- Use `client.subscribe(topic, [opts], listener)` to *register a listener* for the topic.
135
-
-`opts` are the standard options `mqtt.Client#subscribe()` supports
136
-
- While `mqtt.Client#subscribe()` supports an `Array` of topics, our `topic` is singular, and must be a string.
208
+
-`opts` are the standard options `MqttClient#subscribe()` supports, including `decoder`
209
+
- While `MqttClient#subscribe()` supports an `Array` of topics, our `topic` is singular, and *must* be a string.
137
210
- Standard MQTT topic wildcards are supported, and listeners are executed first in order of specificity; i.e. `foo/bar` will take precedence over `foo/+` and `foo/+` will take precedence over `foo/#`.
138
211
- Use `client.unsubscribe(topic, listener)` to remove the listener for the topic.
139
212
- This will not necessarily *unsubscribe* from the topic (at the broker level), because there may be other listeners, but it *will* remove the listener.
140
-
-`client.end()` won't throw a fit if already disconnected
213
+
- If `listener` is omitted, all listeners are removed, which forces unsubscription.
214
+
- Use `client.end(force=false)` to disconnect
215
+
- Use `client.publish(topic, message, [opts])` with standard `MqttClient#publish()` options, including `encoder`
216
+
- Use `connect(url, [opts])` to connect; `url` is a `string`, or you could just pass an `opts` object. Includes `encoder` and `decoder` options, which set the default encoder and decoder, respectively. The default is `text` in both cases.
0 commit comments