Skip to content

Commit 2e845f3

Browse files
cruxicheirosYoseph Maguire
andauthored
chore(README): rework examples to be a bit more specific (mqttjs#1352)
* Change examples to be more specific * Fix ToC * Fixed ToC order Co-authored-by: Yoseph Maguire <yoseph.maguire@microsoft.com>
1 parent c424426 commit 2e845f3

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

README.md

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
MQTT.js is a client library for the [MQTT](http://mqtt.org/) protocol, written
77
in JavaScript for node.js and the browser.
88

9+
## Table of Contents
910
* [__MQTT.js vNext__](#vnext)
1011
* [Upgrade notes](#notes)
1112
* [Installation](#install)
1213
* [Example](#example)
14+
* [Import Styles](#example)
1315
* [Command Line Tools](#cli)
1416
* [API](#api)
1517
* [Browser](#browser)
16-
* [Weapp](#weapp)
1718
* [About QoS](#qos)
1819
* [TypeScript](#typescript)
20+
* [Weapp and Ali support](#weapp-alipay)
1921
* [Contributing](#contributing)
2022
* [License](#license)
2123

@@ -115,6 +117,25 @@ If you do not want to install a separate broker, you can try using the
115117

116118
to use MQTT.js in the browser see the [browserify](#browserify) section
117119

120+
<a name="import_styles"></a>
121+
## Import styles
122+
### CommonJS (Require)
123+
```js
124+
var mqtt = require('mqtt') // require mqtt
125+
var client = mqtt.connect('est.mosquitto.org') // create a client
126+
```
127+
### ES6 Modules (Import)
128+
#### Aliased wildcard import
129+
```js
130+
import * as mqtt from "mqtt" // import everything inside the mqtt module and give it the namespace "mqtt"
131+
let client = mqtt.connect('mqtt://test.mosquitto.org') // create a client
132+
```
133+
#### Importing individual components
134+
```js
135+
import { connect } from "mqtt" // import connect from mqtt
136+
let client = connect('mqtt://test.mosquitto.org') // create a client
137+
```
138+
118139
<a name="promises"></a>
119140
## Promise support
120141

@@ -275,7 +296,7 @@ Connects to the broker specified by the given url and options and
275296
returns a [Client](#client).
276297

277298
The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp',
278-
'tls', 'ws', 'wss'. The URL can also be an object as returned by
299+
'tls', 'ws', 'wss', 'wxs', 'alis'. The URL can also be an object as returned by
279300
[`URL.parse()`](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost),
280301
in that case the two objects are merged, i.e. you can pass a single
281302
object with both the URL and the connect options.
@@ -651,43 +672,6 @@ The MQTT.js bundle is available through http://unpkg.com, specifically
651672
at https://unpkg.com/mqtt/dist/mqtt.min.js.
652673
See http://unpkg.com for the full documentation on version ranges.
653674

654-
<a name="weapp"></a>
655-
## WeChat Mini Program
656-
Support [WeChat Mini Program](https://mp.weixin.qq.com/). See [Doc](https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-socket.html).
657-
<a name="example"></a>
658-
659-
## Example(js)
660-
661-
```js
662-
var mqtt = require('mqtt')
663-
var client = mqtt.connect('wxs://test.mosquitto.org')
664-
```
665-
666-
## Example(ts)
667-
668-
```ts
669-
import { connect } from 'mqtt';
670-
const client = connect('wxs://test.mosquitto.org');
671-
```
672-
673-
## Ali Mini Program
674-
Support [Ali Mini Program](https://open.alipay.com/channel/miniIndex.htm). See [Doc](https://docs.alipay.com/mini/developer/getting-started).
675-
<a name="example"></a>
676-
677-
## Example(js)
678-
679-
```js
680-
var mqtt = require('mqtt')
681-
var client = mqtt.connect('alis://test.mosquitto.org')
682-
```
683-
684-
## Example(ts)
685-
686-
```ts
687-
import { connect } from 'mqtt';
688-
const client = connect('alis://test.mosquitto.org');
689-
```
690-
691675
<a name="browserify"></a>
692676
### Browserify
693677

@@ -806,6 +790,31 @@ Before you can begin using these TypeScript definitions with your project, you n
806790
* Set tsconfig.json: `{"compilerOptions" : {"moduleResolution" : "node"}, ...}`
807791
* Includes the TypeScript definitions for node. You can use npm to install this by typing the following into a terminal window:
808792
`npm install --save-dev @types/node`
793+
794+
### Typescript example
795+
```
796+
import * as mqtt from "mqtt"
797+
let client : mqtt.MqttClient = mqtt.connect('mqtt://test.mosquitto.org')
798+
```
799+
800+
<a name="weapp-alipay"></a>
801+
## WeChat and Ali Mini Program support
802+
### WeChat Mini Program
803+
Supports [WeChat Mini Program](https://mp.weixin.qq.com/). Use the `wxs` protocol. See [the WeChat docs](https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-socket.html).
804+
805+
```js
806+
var mqtt = require('mqtt')
807+
var client = mqtt.connect('wxs://test.mosquitto.org')
808+
```
809+
810+
### Ali Mini Program
811+
Supports [Ali Mini Program](https://open.alipay.com/channel/miniIndex.htm). Use the `alis` protocol. See [the Alipay docs](https://docs.alipay.com/mini/developer/getting-started).
812+
<a name="example"></a>
813+
814+
```js
815+
var mqtt = require('mqtt')
816+
var client = mqtt.connect('alis://test.mosquitto.org')
817+
```
809818
810819
<a name="contributing"></a>
811820
## Contributing
@@ -827,6 +836,7 @@ MQTT.js is only possible due to the excellent work of the following contributors
827836
<tr><th align="left">Siarhei Buntsevich</th><td><a href="https://github.com/scarry1992">GitHub/scarry1992</a></td></tr>
828837
</tbody></table>
829838
839+
830840
<a name="license"></a>
831841
## License
832842

0 commit comments

Comments
 (0)