connect() {
		return new this.broker.Promise((resolve, reject) => {
			let mqtt;
			try {
				mqtt = require("mqtt");
			} catch (err) {
				/* istanbul ignore next */
				this.broker.fatal(
					"The 'mqtt' package is missing. Please install it with 'npm install mqtt --save' command.",
					err,
					true
				);
			}

			const client = mqtt.connect(this.opts);
			this._client = client; // For tests

			client.on("connect", () => {
				this.client = client;
				this.logger.info("MQTT client is connected.");

				this.onConnected().then(resolve);
			});

			/* istanbul ignore next */
			client.on("error", e => {
				this.logger.error("MQTT error.", e.message);
				this.logger.debug(e);

				this.broker.broadcastLocal("$transporter.error", {
					error: e,
					module: "transporter",
					type: C.CLIENT_ERROR
				});

				if (!client.connected) reject(e);
			});

			/* istanbul ignore next */
			client.on("reconnect", () => {
				this.logger.warn("MQTT client is reconnecting...");
			});

			client.on("message", (rawTopic, buf) => {
				const topic = rawTopic.substring(this.prefix.length + this.topicSeparator.length);
				const cmd = topic.split(this.topicSeparator)[0];
				this.receive(cmd, buf);
			});

			/* istanbul ignore next */
			client.on("close", () => {
				this.connected = false;
				this.logger.warn("MQTT client is disconnected.");
			});
		});
	}
Connect to the server

@memberof MqttTransporter