Skip to content

Latest commit

 

History

History
65 lines (48 loc) · 1.34 KB

README.md

File metadata and controls

65 lines (48 loc) · 1.34 KB

cordova-plugin-messager

This is an information tranfer cordova plugin base on MQTT.

Feature

  • Subscribe topic

Install

cordova plugin add cordova-plugin-messager or ionic plugin add cordova-plugin-messager

Usage

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);

Example

init and subscribe topice

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);
});

publish message in terminal

  1. First, you need install mosquitto.
  2. Launch you app.
  3. Execute mosquitto_pub -h test.mosquitto.org -p 1883 -t nt/test1 -m tes1 in the terminal, that will publish a message.
  4. Will alert a message test1.

LICENSE

MIT LICENSE