1.1.0 • Published 7 years ago

enapso.server.client v1.1.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
7 years ago

Introduction

HTTP/WebSocket Client (JavaScript) for Enapso Enterprise Server.

Installation

npm install --save enapso-client-js

Getting Started

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.

  1. 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 WsClient({
      url: 'wss://heprdlxdemo01.innotrade.com', // your remote Enapso server instance
      username: 'guest',
      password: 'guest'
    });
  2. Opening connection and login:

    conn.open().then(() => {
      return conn.login();
    }).then(() => {
      // here authenticated ...
    }).catch(console.error);
  3. 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);
  4. Logout and close connection

    conn.logout().then(() => {
      return conn.close();
    }).then(() => {
      // here closed ...
    }).catch(console.error);
  5. 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);

Config params

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

API

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!'); });

Triggered events

conn.on('message', (msg) => {
  // triggered when a new message arrives from the server
  // argument msg is a JSON object
});

Tests

$ git clone https://github.com/innotrade/enapso-client-js.git
$ cd enapso-client-js/
$ npm install
$ npm test
1.1.0

7 years ago