Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including aliyun-iot-mqtt with all npm packages installed. Try it out:

var aliyunIotMqtt = require("aliyun-iot-mqtt")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

aliyun-iot-mqtt v0.0.4

阿里云IoT物联网套件客户端

aliyun-iot-mqtt

npm-version travis-ci coverage npm-download

Aliyun IoT Hub MQTT client for Node.js

Installation

You can install it as dependency with npm.

$ # save into package.json dependencies with -S
$ npm install aliyun-iot-mqtt -S

Usage

Aliyun IoT Hub mqtt client with authrozied by productKey & deviceName & deviceSecret.

GET Data

const Mqtt = require('aliyun-iot-mqtt');

const client = Mqtt.getAliyunIotMqttClient({
    productKey: "",
    deviceName: "",
    deviceSecret: "",
    regionId: "cn-shanghai",

    keepalive:120 // mqtt options
});


client.on('connect', function() {
    console.log("connect")
})

client.end(function (){
    console.log("end")
})

TLS mqtts

aliyun_iot_root.cer

var trustedCA = fs.readFileSync(path.join(__dirname, '/aliyun_iot_root.cer'))

var options = {
    productKey: "",
    deviceName: "",
    deviceSecret: "",
    regionId: "cn-shanghai",
    protocol: 'mqtts',
    ca: trustedCA,

    keepalive:120 // mqtt options
};

Subscribe Topic

client.subscribe(topic)

Publish Message

client.publish(topic, 'Hello mqtt')
client.publish(topic, 'Hello mqtt', { qos: 1 })

Receive Message

client.on('message', function(topic, message) {
    console.log(topic+"," + message.toString())
})

Bugs

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free