Last active
December 21, 2018 06:10
-
-
Save osk2/cee060fcc817f8c7f1f51825b8bcf9eb to your computer and use it in GitHub Desktop.
Using Eclipse Paho JavaScript Client in Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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