Connect to JSON-RPC server via javascript

I'm starting a MIDI based control surface to Guitarix using Open Stage Control. However, some important actions such as adding or deleting plugins can't be done with MIDI. They can be done with JSON-RPC, and I have a working Python script example that performs these functions.

Does anyone have recommendations for how to send calls and notifications (and sometimes return data) over JSON-RPC using scripts in Open Stage Control? There are various libraries available for javascript JSON-RPC clients, but I haven't used them, and I'm not sure how third party libraries are handled in Open Stage Control scripts.

Thanks.

Interesting, Guitarix, will have to look into that one!

As to your questions, i think your best bet is to use nodejs(comes with open stage control)

I just have used nodejs for local file parsing, you can find a example in the documentation.

Hope this helps.

You"ll need to write a custom module where the nativeRequire function will allow you loading nodejs modules and do pretty much anything you want.

Thanks abstrus, I've seen some nodejs libraries, and that stackoverflow example gives a nice ground-up method.

Jean-Emmanuel, I browsed enough topics last night to know you were going to suggest a custom module! Thanks for the insight to use nativeRequest.

O-S-C is really great software, thanks for developing and maintaining it.

OK, I've got a basic (ugly) prototype Custom Module working. Thank you for the suggestions.

Here it is for the curious:

// Import net and event-stream modules.
var net = nativeRequire('net');
var es = nativeRequire('event-stream')

// Class to create a JSON-RPC TCP client.
function Conn (name, host, port){
		this.name = name;
		this.host = host;
		this.port = port;
	    var Addr = {
	        host: this.host,
	        port: this.port
	    }
	    // Create TCP client.
	    var client = net.createConnection(Addr, function () {
	        console.log('Connection name : ' + this.name);
	        console.log('Connection local address : ' + client.localAddress + ":" + client.localPort);
	        console.log('Connection remote address : ' + client.remoteAddress + ":" + client.remotePort);
	    })
	  //Report changes of state and errors.
    client.on('end',function () {
        console.log(this.name,'Client socket disconnect. ');
    });
    client.on('timeout', function () {
        console.log(this.name,'Client connection timeout. ');
    });
    client.on('error', function (err) {
        console.error(this.name,JSON.stringify(err));
    });
	    return client;
	}

	
	Cnotify = function(client, methd, prams) {
		var mesg={
			jsonrpc:"2.0",
			method: methd,
			params: prams,
		}
		client.write(JSON.stringify(mesg)+'\n');
	}
	
	Ccall = function (client, Id, methd, prams) {
		var mesg={
			jsonrpc:"2.0",
			id: Id,
			method: methd,
			params: prams,
		}
		client.write(JSON.stringify(mesg)+'\n');
	}
	
	Osc2rpc = function(data) {
		 var {address, args, host, port, clientId} = data
		 dotrpckey = address.replace(/\//g,'.');
		 rpckey = dotrpckey.substr(1, dotrpckey.length);
		 rpcvalue = args[0].value;
		 rpcparam = {
			 params:[rpckey, rpcvalue],
		 };
		 return rpcparam
	}
	
	Rpc2osc = function(data) {
		for (i = 0; i < data.params.length; i+=2) {
			 osckey = data.params[i].replace(/\./g,'/');
			 oscout = data.params[i+1];
			 receive('localhost', 7000, '/'+osckey, oscout);
		 }
	}



// Create a guitarix client socket.
gxClient = new Conn('guitarix', 'localhost', 7000);

 // Receive stream from server and process as newline sperated JSON-RPC objects
       gxClient
         .pipe(es.split(null, null, {trailing: false}))
         .pipe(es.parse())
         .on('data', function (data) {
			 //console.log(data);
			 if (data.id === "pList") {
				 pListArray = data.result;
				 console.log('pListArray populated');
				 console.log('pListArray size = ' + pListArray.length)
			 } else {
				 if (('result' in data) && (typeof data.result === 'object')) {
					 //console.log(data);
					 for ( var key in data.result) {
						 osckey = key.replace(/\./g,'/');
						 console.log('/' + osckey,' ',data.result[key]);
					 }
				 }
				  if (('params' in data) && !(data.params[0].includes('.v'))) {
				 console.log(data);
					 Rpc2osc(data);
			 }
			 }
          })

module.exports = {
	oscOutFilter:function(data){
        // Filter outgoing osc messages
        rpc = Osc2rpc(data);

       Cnotify(gxClient, 'set', rpc.params);

        return
    },
}


Cnotify(gxClient, 'listen', ["all"]);