Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Enable connections to secure MQTT brokers #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ settings = {
json: false,
mqtt: require('mqtt'),
host: '127.0.0.1',
port: 1883
port: 1883,
keyPath: 'path/to/cert.key',
certPath: 'path/to/cert.cert',
rejectUnauthorized: true
};

ascoltatori.build(settings, function (ascoltatore) {
Expand Down
18 changes: 14 additions & 4 deletions lib/mqtt_ascoltatore.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var SubsCounter = require("./subs_counter");
* - `port`, the port to connect to;
* - `host`, the host to connect to;
* - `mqtt`, the mqtt module (it will automatically be required if not present).
*
* - `keyPath`, path to TLS-key (optional, used for connecting to a secure broker).
* - `certPath`, path to TLS-certificate (optional, for connecting to a secure broker).
* - `rejectUnauthorized`, Reject unauthorized connections (optional, for connecting to a secure broker).
* @api public
* @param {Object} opts The options object
*/
Expand Down Expand Up @@ -50,15 +52,23 @@ MQTTAscoltatore.prototype = Object.create(AbstractAscoltatore.prototype);
*/
MQTTAscoltatore.prototype._startConn = function() {
var that = this,
settings = null;
settings = null,
createMethod = 'createClient';
if (this._client === undefined) {
settings = {
keepalive: that._opts.keepalive,
clientId: that._opts.clientId,
clientId: that._opts.clientId
};

if(that._opts.keyPath) settings.keyPath = that._opts.keyPath;
if(that._opts.certPath) settings.certPath = that._opts.certPath;
if(that._opts.rejectUnauthorized) settings.rejectUnauthorized = that._opts.rejectUnauthorized;
if(settings.certPath || settings.keyPath) {
createMethod = 'createSecureClient';
}

debug("connecting..");
this._client = that._opts.mqtt.createClient(that._opts.port, that._opts.host, settings);
this._client = that._opts.mqtt[createMethod](that._opts.port, that._opts.host, settings);
this._client.setMaxListeners(0);
this._client.on("connect", function() {
debug("connected");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"sinon": "~1.7.3",
"sinon-chai": "~2.4.0",
"optimist": "~0.6.0",
"async_bench": "~0.1.0",
"async_bench": "~0.3.0",
"dox-foundation": "0.5.4",
"mosca": "git://github.com/mcollina/mosca.git",
"jshint": "~2.3.0",
Expand Down