an implementation of a OPC UA stack fully written in javascript and nodejs
node-opcua is an experimental OPC-UA stack written in NodeJS.
Why NodeJS ?
Because nodeJs is a great framework to design asynchronous application.
$ npm install node-opcua
install pre-requisite:
$ npm install async node-opcua
create a file myfirstclient.js
and add the following code :
var opcua = require("node-opcua");
var async = require("async");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname() + ":4841";
var the_session = null;
async.series([
// step 1 : connect to
function(callback) {
client.connect(endpointUrl,function (err) {
if(err) {
console.log(" cannot connect to endpoint :" , endpointUrl );
} else {
console.log("connected !");
}
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession( function(err,session) {
if(!err) {
the_session = session;
}
callback(err);
});
},
// step 3 : browse
function(callback) {
the_session.browse("RootFolder", function(err,browse_result,diagnostics){
if(!err) {
browse_result[0].references.forEach(function(reference) {
console.log( reference.browseName);
});
}
callback(err);
});
},
// step 4 : read a variable
function(callback) {
the_session.readVariableValue("ns=2;s=Furnace_1.Temperature", function(err,dataValues,diagnostics) {
if (!err) {
console.log(" temperature = " , dataValues[0].value.value);
}
callback(err);
})
},
], function(err) {
if (err) {
console.log(" failure ",err);
} else {
console.log("done!")
}
// disconnect regardless
client.disconnect(function(){});
}) ;
now run it
$ node myfirstclient.js
$ git clone git://github.com/erossignon/node-opcua.git node-opcua
$ cd node-opcua
$ npm install
$ npm test