HTTP/WebSocket Client (JavaScript) for Enapso Enterprise Server.
npm install --save enapso-client-js
For this example we will use a production installation of the Enapso Enterprise Server, this is the Enapso Dash platform. The library use ES6 Promise based APIs.
- Creating connection instance:
// HTTP connection
const HttpClient = require('enapso-client-js').HttpClient;
const conn = new HttpClient({
url: 'https://dash.innotrade.com/http',
username: 'guest',
password: 'guest'
});
// WebSocket connection
const WebSocketClient = require('enapso-client-js').WebSocketClient
const conn = new WebSocketClient({
url: 'wss://heprdlxdemo01.innotrade.com', // your remote Enapso server instance
username: 'guest',
password: 'guest'
});
- Opening connection and login:
conn.open().then(() => {
return conn.login();
}).then(() => {
// here authenticated ...
}).catch(console.error);
- Making requests to the server:
conn.send({
ns: 'com.enapso.plugins.ontology',
type: 'runSPARQLQuery',
ontologyAlias: 'EnapsoUnits',
query: 'SELECT ?v WHERE {?s ?p ?v} LIMIT 5',
reasoning: false
}).then(console.log).catch(console.error);
- Logout and close connection
conn.logout().then(() => {
return conn.close();
}).then(() => {
// here closed ...
}).catch(console.error);
- Join all together using a more elegant way, the co library:
const co = require('co');
const HttpClient = require('enapso-client-js').HttpClient;
const conn = new HttpClient({
url: 'https://dash.innotrade.com/http',
username: 'guest',
password: 'guest'
});
co(function *(){
// opening connection
yield conn.open();
// login
yield conn.login();
// making request
let response = yield conn.send({
ns: 'com.enapso.plugins.ontology',
type: 'runSPARQLQuery',
ontologyAlias: 'EnapsoUnits',
query: 'SELECT ?v WHERE {?s ?p ?v} LIMIT 5',
reasoning: false
});
console.log(response);
// logout
yield conn.logout();
// close connection
yield conn.close();
}).catch(console.error);
let config = {};
config.url = 'https://dash.innotrade.com/http'; // the Enapso Enterprise Server connection URL
config.username = 'guest'; // login username
config.password = 'guest'; // login password
config.autoSyncTimeout = 500; // timeout used by the HttpClient to automatically pull messages from the server. Min value: 400ms
getId: Get the connection id value. CAUTION: this value should be kept secret.
conn.getId(); // UUID v4 value
getConfig: Get the configuration object passed to the client during initialization.
conn.getConfig();
open: Open the client connection.
conn.open().then(() => { console.log('connected!'); });
login: Login the client connection. Optionally username, password arguments can be passed to override the ones in the configuration. The response argument contains the user authorities/rights.
conn.login().then((response) => { console.log('authenticated!'); });
send: Send a request message to the server.
conn.send({{
ns: 'com.enapso.plugins.ontology',
type: 'runSPARQLQuery',
ontologyAlias: 'EnapsoUnits',
query: 'SELECT ?v WHERE {?s ?p ?v} LIMIT 5',
reasoning: false
}}).then((response) => {});
sync: (Only available on HttpClient) Pull pending messages from the server and trigger the 'message' event if applicable. CAUTION: This feature is internally executed by the HTTP client on periodic intervals. See 'autoSyncTimeout' configuration property.
conn.sync().then((messages) => {});
getStatus: Get the client status: UP, DOWN
conn.getStatus();
logout: Logout the client connection.
conn.logout().then(() => { console.log('logged out!'); });
close: Close the client connection.
conn.close().then(() => { console.log('closed!'); });
conn.on('message', (msg) => {
// triggered when a new message arrives from the server
// argument msg is a JSON object
});
$ git clone https://github.com/innotrade/enapso-client-js.git
$ cd enapso-client-js/
$ npm install
$ npm test