This is an information tranfer cordova plugin base on MQTT.
- Subscribe topic
cordova plugin add cordova-plugin-messager or ionic plugin add cordova-plugin-messager
var messager = new CDVMessager({
clientId: 'clientId', // required
host: 'Your server host', // required
port: 'Your server port', // required
username: 'Your server username', // optional
password: 'Your server password' // optional
});
// subscribe topic
var topic = 'test/topic';
messager.subscribe(topic);
// listen the 'receivedMqttMsg_<topic>' event
messager.on('receivedMqttMsg_' + topic, function (msg) {
alert(msg);
});
// cancelSubscribe topic
messager.cancelSubscribe(topic);
var messager = new CDVMessager({
clientId: 'test',
host: 'test.mosquitto.org',
port: '1883'
});
var topic = 'nt/test1';
messager.subscribe(topic);
messager.on('receivedMqttMsg_' + topic, function (msg) {
alert(msg);
});
- First, you need install
mosquitto
. - Launch you app.
- Execute
mosquitto_pub -h test.mosquitto.org -p 1883 -t nt/test1 -m tes1
in the terminal, that will publish a message. - Will alert a message
test1
.