/**
* Faye client implementation.
*
* Implements faye client to communicate with IdealNet's pubsub
* server.
*
* @author michael whobrey <mwhobrey@idealss.com>
*/
const Faye = require('faye');
const Extension = require('./fayeExtension');
const {constants} = require('./constants');
const {log} = require('./utils');
/**
* Client class wrapping faye client.
*
* Convenience methods and properties to facilitate setting up,
* connecting, subscribing and publishing messages to PubSub.
*/
class INRClient {
#api;
#group;
#siteId;
#token;
#key;
#client;
#extension;
#onSubscribed;
#onError;
/**
* Create a client.
*
* @param _api {string} the api that we are connecting to
* @param _group {string} the group id for the connection
* @param _siteId {string} the site id for the connection
* @param _token {string} the token being used for the connection
* @param _key {string} the private key used for signing messages
*/
constructor(_api, _group, _siteId, _token, _key) {
console.log(`Connecting to pubsub on ${constants.pubsubEndpoint()}.`);
console.log("Client properties: ", {
api: _api,
group: _group,
siteId: _siteId,
token: _token
});
this.#api = _api;
this.#group = _group;
this.#siteId = _siteId;
this.#token = _token;
this.#key = _key;
this.#client = new Faye.Client(constants.pubsubEndpoint(), {timeout: 120});
this.#extension = new Extension(_api, _group, _siteId, _token, _key);
this.subscribe = this.subscribe.bind(this);
this.publish = this.publish.bind(this);
this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this);
}
/**
* Getter for #api.
*/
get api() {
return this.#api;
}
/**
* Getter for #group.
*/
get group() {
return this.#group;
}
/**
* Getter for #siteId.
*/
get siteId() {
return this.#siteId;
}
/**
* Getter for #token.
*/
get token() {
return this.#token;
}
/**
* Getter for #key.
*/
get key() {
return this.#key;
}
/**
* Getter for #client
*/
get client() {
return this.#client;
}
/**
* Setter for #client.
*
* @param value {Client} The value for client.
*/
set client(value) {
this.#client = value;
}
/**
* Getter for #state.
*/
get state() {
return this.#client._state;
}
/**
* Getter for #extension.
*/
get extension() {
return this.#extension;
}
/**
* Setter for #extension.
*
* @param value {Extension} The extension we are setting.
*/
set extension(value) {
this.#extension = value;
}
/**
* Setter for #onSubscribed.
*
* @param value {Function} The callback to call.
*/
set onSubscribed(value) {
this.#onSubscribed = value;
}
/**
* Getter for #onSubscribed.
*/
get onSubscribed() {
return this.#onSubscribed;
}
/**
* Setter for #onError.
*
* @param value {Function} The callback to call.
*/
set onError(value) {
this.#onError = value;
}
/**
* Getter for #onError.
*/
get onError() {
return this.#onError;
}
/**
* Subscribe to a channel.
*
* This function allows subscribing to a channel and processing
* the direct response from PubSub (note this is not the same as
* processing the response from another client). Also handles updating
* the extension to let it know the state of the subscription and set
* the property appropriately. Logs result to console.
*
* @param channel {string} The channel we are subscribing to.
*/
subscribe(channel) {
this.#client.subscribe(channel,
(msg) => {
log("Subscribe request response: ", msg);
});
}
/**
* Publish a message to a channel.
*
* Publishes the provided data packet to the provided channel and
* waits for the return to return the response to the caller.
*
* @param channel {string} The channel to publish to.
* @param data {Object} Data packet to publish.
*/
publish(channel, data) {
this.#client.publish(channel, data,
function(mess) {
log('PUBLISH RESPONSE:', mess);
},
function(err) {
log('PUBLISH ERROR:', err);
});
}
/**
* Connect to the faye server.
*
* Setup client and connect to PubSub faye server
*
* @param messageBack {Function} Function to be called when the response from a sent message
* comes back.
*/
async connect(messageBack) {
if (!this.#client) {
this.#client = new Faye.Client(constants.pubsubEndpoint(), {timeout: 120});
}
if (!this.#extension) {
this.extension = new Extension(this.#api, this.#group, this.#siteId, this.#token, this.#key);
}
if (!this.#client._extensions ||
(this.#client._extensions && this.#client._extensions.length === 0)) {
this.#client.addExtension(this.#extension.extend(messageBack));
}
this.#extension.doSubscribe = this.subscribe;
this.#extension.onSubscribed = this.#onSubscribed;
this.#extension.onError = this.#onError;
await this.#client.connect();
}
/**
* Disconnect from server.
*
* Disconnect the client from PubSub server.
*/
disconnect() {
this.#client.disconnect();
}
}
module.exports = INRClient;