How to use the LiveView API from NodeJS

How to use the LiveView API from NodeJS

book

Article ID: KB0083833

calendar_today

Updated On:

Products Versions
TIBCO Live Datamart -

Description

We are using NodeJS as our backend HTTP API server for sending data to clients. We need to fetch data from LiveView tables and send it back in response. For this we need to connect to LiveView from NodeJS.

The liveview.js file provided by the LiveView JavaScript API is browser based code. It has many references to window, jquery, DOM, and other facilities which are available in the browser but not in node.js server side code.

Issue/Introduction

Steps to provide browser facilities required by the LiveView JavaScript API.

Resolution

At the time of this writing (July 2017) the solution is to provide the window and document structure currently required by the LiveView JavaScript API. Future enhancements to the product may have replaced this functionality. Please check the product documentation.

Use the following libraries:
1. jsdom : This library provides a headless browser DOM in node.js (required by jQuery)
2. ws : This library provides websocket support in node.js
3. atmosphere.js : This is the atmosphere wrapper over websockets
4. xmlhttprequest-cookie : This library provides an AJAX implementation which supports cookies
5. jQuery : This library provides document traversal and a simplified AJAX API

Initialize the above libraries as shown below:

//load JSDOM
var jsdom = require('jsdom').jsdom;

//create a empty document
document = jsdom('<html><head></head><body></body></html>');

//create a window from the document
window = document.defaultView;

//add an error object
window.Error = Error;

//create and set navigator
navigator = window.navigator = {};
navigator.userAgent = 'NodeJS JSDom';
navigator.appVersion = '';

//btoa function
window.btoa = function(str){
    return (new Buffer(str)).toString('base64');
};
//atob function
window.atob = function(data){
    return (new Buffer(data, 'base64')).toString('ascii');
};

//load websocket library (used by atmosphere)
//initialize a global variable since the atmosphere library looks for WebSokect NOT window.WebSocket
WebSocket = require("ws");

//set it as WebSocket to the window (browsers do the same)
window.WebSocket = WebSocket;

//load jQuery
jQuery = require(<load jquery from file system>);
var atmosphere = require(<load atmosphere.js from file system>);
window.atmosphere = atmosphere;

//load the source code
require(<load liveview.js>);

//load and set a XHRHttpRequest implementation for jQuery to use for ajax calls
//we need cookie support since live view server authentication is based on http sessions
//so the JSESSIONID cookie needs to be passed back and forth
var XMLHttpRequest = require('xmlhttprequest-cookie').XMLHttpRequest;

//enable CORS support in jquery to allow ajax queries
jQuery.support.cors = true;

//set our xml http request implementation in jQuery
jQuery.ajaxSetup({
    xhr: function(){
        return new XMLHttpRequest();
    }
});

//perform connect from the window object
window.LiveView.connect({
            url: <ldm_url> + '/lv/client',
            username: username, //give credentials, if auth is enabled
            password: password, //give credentials, if auth is enabled
            transportUrl: http://localhost:10080, //use http://username:password@locahost:10080 if auth is enabled
            context: window
    })
    .then(
        function(connection){
            //use the connection as documented in JS docs and then close the connection 
        },
        function(error){
            //handle error connecting to server 
        }
    );
);