Calling VistA Remote Procedure Call in EWD.js

EWD.js is an Open Source framework for development of browser-based applications.  EWD.js provides access to MUMPS databases and we can call an M function from a client and get results back. If we decide to use EWD.js for our next VistA application, how can we access VistA? The answer is: there are multiple ways in our disposal.

Most developers, who write applications for VistA, use VistA Remote Procedure Call (RPC) to send and retrieve data to and from VistA. Most likely they connect to VistA through RPC Broker. The RPC Broker is available for Delphi, Java, C#, etc. What should we do if we want to call RPCs in EWD.js? One way is to find the underlying M entry point of the RPC and to call it directly with appropriate arguments.  But applying this approach at least bypasses the security that is part of the RPCs infrastructure.

A solution

Instead of re-writing RPC Broker in JavaScript, we can leverage the MUMPS database communication layer provided by EWD.js.

On the EWD.js server side we can:

  • Set the RPC information in a temporary M global.
  • Call an M function that executes the RPC on VistA side.
  • Parse the results.

The M function is a wrapper routine that implements the RPCs execution.  The wrapper parses the RPC information stored in the temporary M global and calls the underlying M entry point of the RPC.  The result is set in temporary M global, which EWD.js server parses and processes.

An implementation

A function that handles an RPC call on EWD.js server side is shown below:

rpcExecute = function(ewd, pRPC) {

	var ewdtemp = new ewd.mumps.GlobalNode('TMP',['ewd',process.pid]);
	ewdtemp.$('RPC')._delete();
	ewdtemp.$('RPC')._setDocument(pRPC);
	var temp = getNA(ewdtemp._node,"RPC");  // e.g., ^TMP("ewd","3740","RPC")

	var str = ewd.mumps.function("rpcExecute^nstRPCWrapper", temp);
	var result = JSON.parse(str);
	if (result.success) {
		result.result = ewdtemp.$('RPC').$('result')._getDocument();
	};
	ewdtemp.$('RPC')._delete();

	return result;
};

The input parameter pRPC is a JSON object with attributes:

  • name – the RPC name
  • duz – the VistA user id (DUZ)
  • division – the VistA division id to be used ( DUZ(2) )
  • context – the VistA RPC context in which the RPC will be executed
  • input – JSON array that represents the RPC input parameters. Each record in the array has     attributes type and value, where type is the RPC input type as defined in FileMan PARAMETER TYPE field (#8994.02, .02)

Below are listed a few examples of the pRPC input parameter and the corresponding result of rpcExecute(ewd, pRPC) function.

pRPC Result
{
 "name": "XWB EGCHO STRING",
 "duz": "1",
 "division": "",
 "context": "",
 "input": [
   {
     "type": "LITERAL",
     "value": "Hello RPC!"
   }
 ]
}
{
 "success": 1,
 "message": "",
 "result": {
     "type": "SINGLE VALUE",
     "value": "Hello RPC!"
  }
}
{
 "name": "DDR GETS ENTRY DATA",
 "duz": "1",
 "division": "",
 "context": "",
 "input": [
   {
     "type": "LIST",
     "value": {
       "FILE": "200",
       "FIELDS": ".01",
       "IENS": ".5,",
       "FLAGS": "IE"
     }
   }
 ]
}
{
 "success": 1,
 "message": "",
 "result": {
   "type": "ARRAY",
   "value": {
     "1": "[Data]",
     "2": "200^.5^.01^POSTMASTER^POSTMASTER"
   }
 }
}
{
 "name": "XWB EGCHO SORT LIST",
 "duz": "1",
 "division": "",
 "context": "",
 "input": [
   {
    "type": "LITERAL",
    "value": "HI"
    },
    {
     "type": "LIST",
     "value": {
               "1": "",
               "5": "",
               "2": ""
              }
      }
    ]
}
{
 "result": 1,
 "message": "",
 "result": {
    "type": "ARRAY",
    "value": {
       "1": "5",
       "2": "2",
       "3": "1"
     }
  }
} 

The complete source code of the implementation can be found on git hub.

So, to call an RPC in EWD.js we just need to set a JSON object with attributes described and execute the rpcExecute(ewd,pRPC) function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: