Your first NodeJS install

As HubConnect v2.0 (aka v1.7) nears Release with it's HubConnect Server, it may be time to review what is needed to get a NodeJS environment installed and ready for the new Release.

Many people have a NodeJS environment running, since it's needed for many other tools. Homebridge is a solid example. If you have Homebridge working, you already have a perfectly good environment for installing HubConnect Server side-by-side with Homebridge.

If it's your first time, then there are plenty of How-To documents and Videos available for your specific always-on computer.

Understand that it's a multi-part install. NodeJS first, which includes NPM (Node Package Manager) that will be used to install HubConnect Server.

First, visit

NodeJS.org to get the install binary for your chosen platform. You will want "LTS" and a "binary" (Don't go down the Source trail.) For Windows or Mac OSX, just click the large 'installer' button and you will instantly begin the download. For Raspberry Pi, you will click on the "Linux Binaries (ARM)" row for the processor in your rPi. A Raspberry Pi 3B+ uses ARM 7, for example.

There are so many How-To guides, it's impractical to identify "the best" for your environment, but you may want to try these as a starting point:

Raspberry Pi:

Windows:

Mac OSX:

All of the install instructions tell you to verify NodeJS by simply typing (at a command line):

node -v

and confirming you get something LIKE:

$ node -v
v12.13.0

and NOT like:

$ node -v
-bash: node: command not found

:slight_smile:

It's a good time to also verify NPM is functional too:

$ npm -version
6.13.4

More to Follow as HubConnect v1.7 moves into Release.

Anyone got a BETTER How-To than mine? :slight_smile:

11 Likes

There's a great install script that I use for my RPi's to install node. It allows you to move from one version to another quite easily as well. Find it very useful when I'm trying to figure out if the Node version is what's screwing me up.

1 Like

Are there any nodeJs version requirements. I'm using v8.

2 Likes

v8 is quite old. I tested with v12.13

I'm trying a downgrade now on my rPi...

1 Like

Thanks. I got other apps that need v8. Not sure how easy it will be to install both versions.

You can't run both version at once on the same pi in the native OS. You would have to run one in a docker container.

2 Likes

I thought so. I remember having to downgrade.

csteele@raspberrypi3:~/.HubConnectProxy$ node proxy.js
03:13:36 HubConnect Websocket Proxy Server v0.0.3
Copyright 2019 Retail Media Concepts LLC
All Rights Reserved

03:13:36 Initializing configured hubs...

03:13:37 Connection worker for Server online (PID 5368)
03:13:37 Connection worker for ZeeRadioLower online (PID 5373)
03:13:37 Connection worker for ZeeRadioUpper online (PID 5374)
03:13:37 Server: Initializing websocket server...
03:13:37 ZeeRadioLower: Initializing websocket server...
03:13:37 ZeeRadioUpper: Initializing websocket server...
03:13:37 Server: Connecting to Hubitat websocket...
03:13:37 ZeeRadioLower: Connecting to Hubitat websocket...
03:13:37 ZeeRadioUpper: Connecting to Hubitat websocket...
03:13:38 Server: Connected!
03:13:38 ZeeRadioUpper: Connected!
03:13:38 ZeeRadioLower: Connected!
^C
csteele@raspberrypi3:~/.HubConnectProxy$ node -v
v8.17.0
csteele@raspberrypi3:~/.HubConnectProxy$

1 Like

Is there a recommended docker to run node? If it’s tailored for unraid even better.

1 Like

Well, you'd have to build a container with your app too. There are a ton of different docker containers with node for you to start with. But that's just the starting point. It's not like using a virtual machine. I found that one the hard way.

It probably won't be the first thing produced but @Dan.T provided @srwhite and I some good hints for building a docker. The more we build on similar experiences, I think the better the Community assistance can be.

Because I've practically "worn the carpet thin" with installing NodeJS, etc. I see it as 'easy'. Which is exactly the opposite of my FIRST attempt. Which is the motivation for starting this thread... You've got a couple weeks of Closed Beta before @srwhite releases 1.7 and in that time, almost anyone can overcome the 'strangeness' that is NodeJS. Strange if only because the words are new. Node? NodeJS? NPM? N? what the heck? :smiley:

I have never used Docker... beyond installing it and getting "Hello World" working... then deleting it all. I wasn't planning on THIS being my first attempt either :slight_smile:

I run various node apps in unraid docker containers. Once the server is released I'll try to find time to make a container for unraid. At minimum I can post an unraid how-to, though.

Takes about 10 seconds. A simple way is to start w/a node container, copy the app to the container, and add the command to run the app as part of the command line / extra parameters.

But you can also make pre-built containers with node and the app, too, obviously.

2 Likes

Is that a "thing"? is there a repo of containers pre built for various popular apps?

Oh yeah....definitely a thing.
http://hub.docker.com/

If you want to run a container using just the docker run command, it has to be on docker hub. Otherwise you have to download the image file manually. if you link a github repo to a docker hub image, it can even autobuild an updated image when you commit to github.

1 Like

Don't build one just for me but I would guess that others would use it.

1 Like

Well, yeah. It's MUCH easier to deploy. You only need a one line command to run the app. No installing node js or worrying that you have the right node js version or any of that.

I have an ancient chunk of code that may be a good way to test the next layer of building a first NodeJS environment.

You'll want to create a directory for this and it may as well be the same pattern as Homebridge uses... a 'hidden' directory with all the good stuff within.

OSX and rPi can type from a command line:

cd ~
mkdir .HubConnectServer
cd .HubConnectServer
nano proxy.js

Then paste in:

/* Websocket Proxy. 

    listens1 to Hubitat WS and proxies it to :8010
    listens1 to :8010 and proxies it to Hubitat WS
*/

const WebSocket = require('ws')
var   url = 'ws://192.168.0.200:80/eventsocket'
const listens1 = new WebSocket(url)
console.log('Listening...')

console.log("Server started");
var WebSocketServer = require('ws').Server
    , wss = new WebSocketServer({port: 8010});
    wss.on('connection', function(ws) {
        listens1.on('message', function(mx) {
        console.log('1: %s', mx);
        ws.send(mx);
    });
        ws.on('message', function(mz) {
        console.log('S: %s', mz);
        listens1.send(mz);
    });
 });

listens1.onerror = (error) => {
  console.log(`WebSocket error: ${error}`)
}

then:

nano package.json

and paste in:

{
  "name": "eventsocket-proxy",
  "version": "0.0.1",
  "description": "Proxy websocket router for Hubitat.",
  "main": "proxy.js",
  "dependencies": {
    "websocket-relay": "^0.3.3"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "C Steele",
  "license": "ISC"
}

then it's time to let NPM install all the dependencies...

npm install

you'll see something like:

$ npm install
npm WARN eventsocket-proxy@0.0.1 No repository field.

audited 9 packages in 1.582s
found 0 vulnerabilities

Then it's time to test...

$ node proxy.js
Listening...
Server started

Nothing else happens, you can ^c out and be pretty happy.

What you don't want to see is:

$ node proxy.js
internal/modules/cjs/loader.js:797
throw err;
^

Error: Cannot find module 'ws'
Require stack:

* /home/csteele/proxy.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/home/csteele/proxy.js:7:19)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) {
code: 'MODULE_NOT_FOUND',

Note: the above is from a RaspberryPi 3B+ which is to say, your milage may vary. :slight_smile:
I didn't compare, but my memory of doing the same on my Mac is that it's the same.

1 Like

If you have this in a repo that could be cloned, that would be a lot easier.

You don't find on the pi you have to run your npm install with sudo? I've had very limited success with any node.js implementation without either su'ing or using sudo.

@csteele

Thank you very much! node v8.10.0 works - I'm all good!

1 Like

Please be aware though, that my testing will be done on v12.13 or better..

1 Like

Download the Hubitat app