connect() {
		return new this.broker.Promise((resolve, reject) => {
			let Stan;
			try {
				Stan = require("node-nats-streaming");
			} catch (err) {
				/* istanbul ignore next */
				this.broker.fatal(
					"The 'node-nats-streaming' package is missing! Please install it with 'npm install node-nats-streaming --save' command.",
					err,
					true
				);
			}
			const client = Stan.connect(this.opts.clusterID, this.nodeID, this.opts);
			this._client = client; // For tests

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

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

			/* istanbul ignore next */
			client.on("reconnect", () => {
				this.logger.info("NATS Streaming client is reconnected.");
				this.onConnected(true);
			});

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

			/* istanbul ignore next */
			client.on("disconnect", () => {
				if (this.connected) {
					this.logger.warn("NATS Streaming client is disconnected.");
					this.connected = false;
				}
			});

			/* istanbul ignore next */
			client.on("error", e => {
				this.logger.error("NATS 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("close", () => {
				this.connected = false;
				// Hint: It won't try reconnecting again, so we kill the process.
				this.broker.fatal("NATS Streaming connection closed.");
			});
		});
	}
Connect to a NATS Streaming server

@memberof StanTransporter