Tags
API library, HelloWorld, javascript, JDeveloper, Node, node.js, npm, Oracle, prowl, push notifications
So I have written a couple of blogs about Push Notifications with a bit of Java (see here as the post that pulls all of this together). But this time we’re going to do something similar with Node.js. This blog entry is going to position us so we can then take a simple solution and push up to the cloud – as I use Oracle a lot then we’ll be looking at the Oracle cloud as a final step.
To start we need a local instance of node.js. Given the fact it is a small footprint we can pretty much install anywhere. So you’ll need to download Node.JS from the official site, and install it. I’m not going to walk through the installation guidance as it is well documented elsewhere (http://blog.teamtreehouse.com/install-node-js-npm-windows for example). You do want ensure you include the NPM capability (node.js packaging & deployment tool). Make sure that Node is on your path so we can reference the binary without a lots of file paths. You also want to ensure that node.js is up and running.
Next up is to the the Prowl API library that makes interacting with prowl simple and helps illustrate the deployment framework (NPM) used by node.js. So following the link from the Prowl website or go directly here and download the library. If you download the zip file as I did, you’ll find it has a folder called node-prowl-master. You need to unpack this and rename to node-prowl. and run the command
npm install node-prowl
When I first tried to deploy the Prowl API library then I did see an error. This isn’t the API but actually the Node.js installation (atleast on my Windows platform) as you can see:
I found googling using node.js and ENOENT showed up plenty of help to solve errors. In this situation the solution was purely to create the folder. Then re-running the action without problem.
When the npm command works you’ll see something like:
So hopefully in addition to the prerequisites described in this earlier post we should have everything ready to progress. So I’ve continued to use JDeveloper 12c, but using the general profile and set up a web solution project. This does create a large directory structure given we’re producing some simple Javascript. But the structure is right for a proper development effort, and lazy habits form poor practises – so lets work with it.
With the project setup, we need craft a little JavaScript. To we’re good to go – lets just try hello world, with a tiny twist, we’ll get the hostname using a Node library with this code:
// our very first node program
// get info about the OS
var os = require(‘os’);// say hello world and include the hostname
console.log(“Hello world, we’re running on ” + os.hostname());
Before we do anything else, lets be a bit clever, to allow us to run our Node script within JDeveloper. This can be done by adding a new Tool through the Tools –> External Tools … menu. Which will display the following screen:
Asa you can see in this image I have already selected New… and walked through the configuration screens, you’ll probably want to use a configuration similar to what I have in the following steps:
With this setup in JDeveloper with the Editor focus on our JavaScript, goto the Tools menu and you’ll see your Node entry. Just click on it. We’ll then see the results in the message window, as you can see here:
Alternatively in a command window you just need to run the command from the folder with the JavaScript (or include the path):
node helloworld.js
So lets take things up a notch and send our mobile device a message. So using the following code, we can use the prowl-api and initiate a message:
var Prowl = require(‘node-prowl’); // pull in the prowl API we deployed with NPM earlier
var prowl = new Prowl(‘your-prowl-key-here‘); //setup your API key
var now = new Date();
// ready to send the message, passing a function reference to handle the response
var message = ‘hello mobile device, the time is ‘+ now.toUTCString();
prowl.push(message, ‘NodeJS App’, prowlReplyHandler);//function to handle the response from the prowl API lib
function prowlReplyHandler ( err, remaining )
{if( err )
{
var errorStr = err.message;
console.log( ‘I have an error ‘ + errorStr);
}
else
{
console.log( ‘I said:’ + message+ ‘; I have ‘ + remaining + ‘ calls available’ );
}}
Note you’ll need to replace your-prowl-key-here in the above code with you genuine API key registered with the Prowl web app. Then we can run the application, and should see:
Our mobile device will show:
Next steps, in the next post – run through through a cloud hosting of node.js and extend the capability to be a simple service, which will mean packaging ourselves up and other exciting things.
Pingback: Oracle Node.js cloud service | MP3Monster's Blog