Skip to content

Instantly share code, notes, and snippets.

@osk2
Last active December 21, 2018 06:10
Show Gist options
  • Save osk2/cee060fcc817f8c7f1f51825b8bcf9eb to your computer and use it in GitHub Desktop.
Save osk2/cee060fcc817f8c7f1f51825b8bcf9eb to your computer and use it in GitHub Desktop.
Using Eclipse Paho JavaScript Client in Node.js
global.WebSocket = require('websocket').w3cwebsocket;
/*
Download `paho-mqtt.js` from https://github.com/eclipse/paho.mqtt.javascript/blob/master/src/paho-mqtt.js
I tried https://github.com/eclipse/paho.mqtt.javascript/blob/1ff91a3e9bda279c855bb951fc32898983f85b3c/src/paho-mqtt.js personally
It should work just fine
*/
global.Paho = require('./paho-mqtt');
const _options = {
clientId: 'clientId',
server: 'wss://test.com',
login: 'account',
password: 'password'
};
const _client = new Paho.Client(_options.server, _options.clientId);
const onMessage = message => {
// Do something with `message`...
}
const onDisconnect = error => {
// Do something with `error`...
}
_client.onMessageArrived = onMessage;
_client.onConnectionLost = onDisconnect;
_client.connect({
userName: _options.login,
password: _options.password,
keepAliveInterval: 10,
onSuccess: () => {
// Do something after connected
},
onFailure: (ctx, errCode, errMsg) => {
console.error(error.errorMessage);
// Do something when error occurred
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment