Using ActiveX Com Components with nodejs. Is it possible - node.js

Is there a way to use any ActiveX com components with nodejs?
Actually, I would never need this but I'm running nodejs on Windows and trying to send ping requests without forking new processes (no such module exists for Windows).
As some Activex components exist for sending ping requests with raw sockets, maybe I can use them.
An example of how you can create a COM object from JavaScript is:
var rs = new ActiveXObject("ADODB.Recordset");

There is node-win32ole ( npm install win32ole ).
EDIT: win32ole is no longer actively maintained. You could try winax instead.

(updated)
You could try to use node-ffi to bind to Win32 and launch a COM/ActiveX component (CoCreateInstance) or access winsock/icmp directly. See https://github.com/rbranson/node-ffi
Or try to ping via WMI, e.g. "select * from win32_pingstatus where Address='...'". See https://npmjs.org/package/wmi

An ICMP ping module for Node.js now exists:
https://npmjs.org/package/net-ping

Years later:
node winax
node win32ole no longer works with up to date node.js versions

Related

Snap7 + Ni Labwindows/CVI

Hi guys i am working on a project where my client wants to use Siemens S7 1200 to control some pneumatic tools and an interface on labwindows Cvi.
I downloaded SP7 ( snap 7) on a try to communicate with my plc but i found myself blocked since the downloaded file contains only a DLL file and a lib file whit no.h file ( header file )
could anyone tell me how to use snap 7 properly on labwindows ?
thanks
AFAIK if you want to use Snap7, you'll have to download the source (C++) and then manipulate it to work with LabWindows.
S7-1200s also have the following communication options.
Free-of-charge:
Modbus/TCP
Socket services ("Open user communication")
MQTT unencrypted
For a fee:
OPC UA
More here:
Communication methods for S7-1200/1500 PLCs
Personal opinion: if you don't have many data points to monitor, I would go with Modbus/TCP.

Handheld Scanner with nodejs and OPOS

I was struggling with this for a couple of days now and Google seem to have no information..
I'm trying to interface a DataLogic-QuickScan-QD2131 scanner using OPOS (under windows 10, RS-232 OPOS interface) with nodejs.
I understood that OPOS uses ActiveX controller to communicate, so i use the winax npm-package to create ActiveXObject reference, but I have no idea what is the "class string" i should provide to the constructor.
Here's my code:
require("winax");
const con = new ActiveXObject("OPOSService.OPOSScanner");
console.log(con);
this will fail with the following error:
Uncaught Error: CreateInstance: OPOSService.OPOSScanner Invalid class string
Thanks guys!
You probably should stop using OPOS from Node.js.
As I answered your other question, the current OPOS only supports 32bit.
If you still want to use it, find and specify the programmatic ID string for the scanner OCX in DataLogic's OPOS.
I don't have any information about what it looks like, so you can find it yourself or contact DataLogic.
As an alternative, obtain and install the Common CO from the following page and specify "OPOS.Scanner" as the programmatic ID.
MCS: OPOS Common Control Objects - Current Version
If you have a combination of Node.js and a barcode scanner in serial port mode, it would be better to send commands and receive barcode data directly from Node.js using the serial port instead of OPOS.

Using node.js modules in electron

I have the following use case:
I want to develop a desktop application, which will run on a Raspberry Pi. I decided to use Electron to build the desktop application and Angular as the frontend framework (1).
I need to use some npm modules for my application in order to communicate with the GPIO-pins or over ethernet tcp communication ('net' node module). I tried to import these in my Angular application (for example import {Socket} from 'net';), but I don't get them working.
I get the following error when trying to construct the net socket like this:
let client = new Socket();
Uncaught (in promise): TypeError: undefined is not a constructor (evaluating 'new net.Socket()')
Is this an error in the base architecture, that I can't run code, which depends on the backend, in the angular frontend. Or am I doing something else wrong?
I also found capacitor (2), which allows to call native sdks. I tried to implement my own plugin, but I get the same error as above.
Thanks for your help.
(1) https://angularfirebase.com/lessons/desktop-apps-with-electron-and-angular/
(2) https://capacitor.ionicframework.com/docs/plugins/
It seems like you have a little confusion about Electron, even thou it is not the classical client:server model, ...
Lets understand Electron first:
Main and Renderer Processes
The main process is for handling/creating BrowserWindows(Renderer) And
for some communication from one renderer-Window to an other one.
(maybe some other special stuff too)
The renderer is where you really run the most of your app. With node,
you have all you need there.
you then notice you will be needing a channel between the renderer process (web page) and the main process.
Don't worry, here is where remote comes in handy :
Use main process modules from the renderer process.
The remote module provides a simple way to do inter-process communication (IPC) between the renderer process (web page) and the main process.
...even the other way round...
Note: For the reverse (access the renderer process from the main
process), you can use webContents.executeJavascript.
So at the end you will be able of using all the magick in both sides.

Meteor.js: How do you require or link one javascript file in another on the client and the server?

1) In node on the backend to link one javascript file to another we use the require statement and module.exports.
This allows us to create modules of code and link them together.
How do the same thing in Meteor?
2) On the front end, in Meteor is I want to access a code from another front end javascript file, I have to use globals. Is there a better way to do this, so I can require one javascript file in another file? I think something like browserify does this but I am not sure how to integrate this with Meteor.
Basically if on the client I have one file
browserifyTest.coffee
test = () ->
alert 'Hello'
I want to be able to access this test function in another file
test.coffee
Template.profileEdit.rendered = ->
$ ->
setPaddingIfMenuOpen()
test()
How can I do this in Meteor without using globals?
Meteor wraps all the code in a module (function(){...your code...})() for every file we create. If you want to export something out of your js file (module), make it a global. i.e don't use var with the variable name you want to export and it'll be accessible in all files which get included after this module. Keep in mind the order in which meteor includes js files http://docs.meteor.com/#structuringyourapp
I don't think you can do this without using globals. Meteor wraps code in js files in SEF (self executing function) expressions, and exports api is available for packages only. What problem do you exactly have with globals? I've worked with fairly large Meteor projects and while using a global object to keep my global helpers namespaces, I never had any issues with this approach of accessing functions/data from one file in other files.
You can use a local package, which is just like a normal Meteor package but used only in your app.
If the package proves to be useful in other apps, you may even publish it on atmosphere.
I suggest you read the WIP section "Writing Packages" of the Meteor docs, but expect breaking changes in coming weeks as Meteor 0.9 will include the final Package API, which is going to be slightly different.
http://docs.meteor.com/#writingpackages
Basically, you need to create a package directory (my-package) and put it under /packages.
Then you need a package description file which needs to be named package.js at the root of your package.
/packages/my-package/package.js
Package.describe({
summary:"Provides test"
});
Package.on_use(function(api){
api.use(["underscore","jquery"],"client");
api.add_files("client/lib/test.js","client");
// api.export is what you've been looking for all along !
api.export("Test","client");
});
Usually I try to mimic the Meteor application structure in my package so that's why I'd put test.js under my-package/client/lib/test.js : it's a utility function residing in the client.
/packages/my-package/client/lib/test.js
Test={
test:function(){
alert("Hello !");
}
};
Another package convention is to declare a package-global object containing everything public and then exporting this single object so the app can access it.
The variables you export NEED to be package-global so don't forget to remove the var keyword when declaring them : package scope is just like regular meteor app scope.
Last but not least, don't forget to meteor add your package :
meteor add my-package
And you will be able to use Test.test in the client without polluting the global namespace.
EDIT due to second question posted in the comments.
Suppose now you want to use NPM modules in your package.
I'll use momentjs as an example because it's simple yet interesting enough.
First you need to call Npm.depends in package.js, we'll depend on the latest version of momentjs :
/packages/my-moment-package/package.js
Package.describe({
summary:"Yet another moment packaged for Meteor"
});
Npm.depends({
"moment":"2.7.0"
});
Package.on_use(function(api){
api.add_files("server/lib/moment.js");
api.export("moment","server");
});
Then you can use Npm.require in your server side code just like this :
/packages/my-moment-package/server/moment.js
moment=Npm.require("moment");
A real moment package would also export moment in the client by loading the client side version of momentjs.
You can use the atmosphere npm package http://atmospherejs.com/package/npm which lets you use directly NPM packages in your server code without the need of wrapping them in a Meteor package first.
Of course if a specific NPM package has been converted to Meteor and is well supported on atmosphere you should use it.

Output to Chrome console from Node.js

I'm looking for a way to output Node variables directly into the google chrome browser console. The same way a console.log() works on the client side. Something like this for php. This would greatly speed up development.
NOTE:
Since the old answer (written in september 2014) refers to an older version of node-inspector, my instructions are not relevant anymore in 2017. Also, the documentation has gotten a lot better, so I have updated my original answer:
node-inspector is what you need.
It opens up an instance of Chrome with its developer tools for debugging.
It's also easy to use:
1. Install
$ npm install -g node-inspector
2. Start
$ node-debug app.js
Source: https://github.com/node-inspector/node-inspector
You might want to try NodeMonkey - https://github.com/jwarkentin/node-monkey
I know it's an old question but came on top of my Google search so maybe somebody will find my answer useful.
So you can use node --inspect-brk index.js
Now, all you have to do is basically just type chrome://inspect in your Chrome address bar and click Open dedicated DevTools for Node
In DevTools, now connected to Node, you’ll have all the Chrome DevTools features you’re used to:
Complete breakpoint debugging, stepping w/ blackboxing
Source maps for transpiled code
LiveEdit: JavaScript hot-swap evaluation w/ V8
Console evaluation with ES6 feature/object support and custom object formatting
Sampling JavaScript profiler w/ flamechart
Heap snapshot inspection, heap allocation timeline, allocation profiling
Asynchronous stacks for native promises
Hope that helped.
The closest thing to this I've seen is Node JS console object debug inspector
See this post for usage and potential issues: http://thomashunter.name/blog/nodejs-console-object-debug-inspector/
For users with nodejs on linux via ssh-shell (putty):
Problem with nodejs on linux-ssh-shell is, that you have no browser connected.
I tried all this solutions, but didnt get it to work.
So i worked out a solution with firebase (https://firebase.google.com), because my project uses firebase.
If you are familiar with firebase, than this is a great way. If not, firebase is worth using in combination with nodejs - and its free!
In the server-side-script (started with node) use a own function log():
// server-side:
// using new firebase v3 !
var fbRootRef = firebase.database();
var fbConsoleRef = fbRootRef.ref("/console");
var log = function(args) {
fbConsoleRef.set({'obj': args});
}
// inside your server-code:
log({'key':'value'});
On client-side you create a firebase-reference on this console-object:
// client side:
fbRootRef.child('/console').on('value', function(d) {
var v = d.val();
console.log(v);
});
Now everything logged on server-side with the log() - function is transferred in realtime to the firebase-database and from there triggering the client-console-reference and logged into the browsers console.
If anyone needs help, i will explain in more detail and could give a more extended version of this logging with types (console./log/warn/info), grouping with title-info (i.e. server says: (filename + line).
Setting up firebase for your project is done in max 30 minutes, inserting the console-function in 30 minutes. I think its worth the time!
You can use bonsole, a simple way to log something in browser. Even in Linux, you can go to the LAN's ip to check it.
The most simple way with least dependencies is using a WebSocket connection to send the messages to the browser. Any WebSocket example you can find on the internet will suffice to accomplish this. Everything else requires to be heavily integrated into the host system and wouldn't work if you want to actually run this on a remote server. You can also send commands to the server directly from the browser console this way.
Links:
https://www.npmjs.com/package/websocket
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

Resources