Node.js is hot and that's not without a reason. There are numerous examples of large websites using Node.js and there is some pretty cool stuff out there.
Since Application Express is officially 'Awesome' and Node.js is 'hot', why not combine the two? With node-oracledb and websockets you can do awesome things, but there is more Node.js has to offer!
In this presentation you will get a short introduction on Node.js. After that I will show you a handful of Node.js applications in Application Express sorted on awesomeness from cool to sub zero.
14. Node.js uses an event-driven, non-blocking I/O model
that makes it lightweight and efficient, perfect for
data-intensive real-time applications that run across
distributed devices.
15. s an event-driven, non-blocking I/O m
kes it lightweight and efficient, perfect
ntensive real-time applications that ru
across distributed devices.
84. oracle = function() {
this.oracledb = require("oracledb");
};
oracle.prototype.insertUser = function(p_username, p_remoteaddress, p_userkey) {
this.oracledb.getConnection({
user: this.db_user,
password: this.db_password,
connectString: this.db_connectionstring
},
function(err, connection) {
//put your code to handle the callback here
});
};
85. oracle = function() {
this.oracledb = require("oracledb");
};
oracle.prototype.insertUser = function(p_username, p_remoteaddress, p_userkey) {
this.oracledb.getConnection({
user: this.db_user,
password: this.db_password,
connectString: this.db_connectionstring
},
function(err, connection) {
//put your code to handle the callback here
});
};
86. oracle = function() {
this.oracledb = require("oracledb");
};
oracle.prototype.insertUser = function(p_username, p_remoteaddress, p_userkey) {
this.oracledb.getConnection({
user: this.db_user,
password: this.db_password,
connectString: this.db_connectionstring
},
function(err, connection) {
//put your code to handle the callback here
});
};
87. function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.action = "insertUser";
connection.module = process.title;
var sqlstatement = "merge into unc_users usr
using (select :username username
,:remoteaddress remoteaddress
,:userkey userkey
from dual) usr1
on (usr.username = usr1.username)
when matched then
update set usr.userkey = usr1.userkey
when not matched then
insert (username, remoteaddress, userkey)
values (usr1.username, usr1.remoteaddress, usr1.userkey)";
88. function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.action = "insertUser";
connection.module = process.title;
var sqlstatement = "merge into unc_users usr
using (select :username username
,:remoteaddress remoteaddress
,:userkey userkey
from dual) usr1
on (usr.username = usr1.username)
when matched then
update set usr.userkey = usr1.userkey
when not matched then
insert (username, remoteaddress, userkey)
values (usr1.username, usr1.remoteaddress, usr1.userkey)";
89. function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.action = "insertUser";
connection.module = process.title;
var sqlstatement = "merge into unc_users usr
using (select :username username
,:remoteaddress remoteaddress
,:userkey userkey
from dual) usr1
on (usr.username = usr1.username)
when matched then
update set usr.userkey = usr1.userkey
when not matched then
insert (username, remoteaddress, userkey)
values (usr1.username, usr1.remoteaddress, usr1.userkey)";
91. function(err, connection) {
if (err) {
console.error(err.message);
return;
}
connection.action = "insertUser";
connection.module = process.title;
var sqlstatement = "merge into unc_users usr
using (select :username username
,:remoteaddress remoteaddress
,:userkey userkey
from dual) usr1
on (usr.username = usr1.username)
when matched then
update set usr.userkey = usr1.userkey
when not matched then
insert (username, remoteaddress, userkey)
values (usr1.username, usr1.remoteaddress, usr1.userkey)";
Oh first of all Node.js
It’s not just a simple framework,
Many large organisations use it to create their websites
LinkedIn used Node.js to create their mobile website
http://venturebeat.com/2011/08/16/linkedin-node/
PayPal stopped using Java and started using Node.js to create their website
https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/
Walmart uses Node.js for their mobile app and uses it as a proxy between different programming API’s
https://www.joyent.com/developers/videos/node-js-at-walmart-introduction
Basicly what PayPal, LinkedIn and Walmart wanted to do is:
Make an mobile application for all the people on the internet
And it has to be fast
Trustworthy
And maybe most of all scalable
And that’s why they chose Node.js
But what is it?
Does anybody know wat node is?
Has anybody seen our LEGO presentation with Node.js?
Well this is what Node.js is.
“Node.js uses an event-driven, non-blocking I/O model
that makes it lightweight and efficient, perfect for
data-intensive real-time applications that run across distributed devices.”
This is the important part.
It’s event driven, so it can react on events.
And it’s non-blocking wich means it’s asynchronous.
And it is JavaScript
Basicly it’s JavaScript on a server
And you can run it from the Oracle cloud if you want
Once you’ve installed node or you use it from the cloud you start with an empty canvas.
So you can start writing your code.
But it’s a lot of work because in order to use for example websockets you will need a webserver and a websocket server.
That’s were the Node Package Manager comes in.
The open source community allready have written a lot of code and you can download these and use them in the NPM
We are only going to use these ones
What I wanted to do in this presentation is send a message from apex to nodejs and back
So i had to define a message
I thought it had to some kind of task the receiver should run
And of course i needed to send some data with it
So in the message there should me a task part and a data part
Finaly i came up with my message with a task and a payload
And now I needed a way to transport the data to node.js
Therefore I used a websocket connection. With the websocket connection I can make a direct connection between a webbrowser and node.js
I have to find some kind a way to send the data over the websocket connection from apex to node.js
That’s where json comes in.
I needed a json message with a task and a payload in it
The task is what we want the other side to do
And the payload is the data you want to send to the other side
Ok but how can we fix this in APEX?
First of all, there are several ways to do this.
One way is to integrate your code into your page. That’s ok if you only use websockets on a single page.
I like to change the page template and add my code for setting up the websocket connection on it
So we navigate to shared components and click on templates.
First we set up a global variable which holds the websocket connection
And then we add the code to set up the connection
This code start with a anonymous javascript function
The first part is to check if you have browser support for websockets
After that I check if there is allready a websocket connection initialized.
If the browser supports websockets, and there isnt a live websocket connection, we create an instance of the websocket
And because node is event driven, we create an event which fires when a message is received.
When a message is received we parse the json message into a javascript object
And then we use this object to fire an apex trigger.
We can catch this trigger with a dynamic action, I will show you later on how to do it.
Well now its fixed in APEX we have to do something on our node.js server.
I’ve allready installed a webserver with the websocket implementation it it.
So all we have to do right now is to implement the event handling of the websocket server.
At first we accept the connection and we save some important information about the connected client.
Then we push the client up the client list
Because it’s event driven we have to implement some events. And it is all done in asynchronous callback functions. You wil see them a lot if you start working with node.
This specific event will fire when the connection receives a message with the name ‘message’
You can put your code to handle the event in this callback function
In example what we are doing is sending a json string
And we do the same thing as we did on the APEX part.
We parse the json message string into a javascript object
And then we can find out easily what kind of task we should run
If we wanted to broadcast some data to all the connected client we will be doing a thing like this.
We loop through the connected clients and contruct a new json string wich we send to all the clients
Ok now we are all experts in using Node.js Im going to show you a few example implementations. But we do it in a special way.
At first i wil explain the idea
Second i will do a live demo
Then i am going to explain the techniques i used
And after all you are rating this example
And we do it topgear style.
Does anybody know topgear?
[explain topgear and the cool wall]
[explain the cool wall]
We won’t be needing the seriously uncool part. Because I can’t image a seriously uncool part of Node.js
So we stick with uncool, cool and subzero
As an example I added the lego presentation to the cool wall. I placed in at sub zero level, i hope you don’t mind.
Let’s start with the first example. I wanted to create a simple checkbox.
I type a message on the screen of my device and the send the message to node
Node is his turn sends the message to all the connected users.
You can use this concept as a chat functionality on your website.
You can use it as a one on many or with little adjustments a one on one example
So what i did in APEX is getting the value of the page item
Construct a JSON string with the task ‘chat_message’ and the message itself as payload
And then sent the message over the websocket connection
I node I used caught the message event and looked for the ‘chat_message’
After that I contruct a new message for the users to receive
And sent it out to all the connected clients
Including myself
Say we have to people looking at a report on their mobile device.
Then some data is added to the report
Then some data is added to the report
And you want both of your reports refreshed.
Well one way to do this is to creeate a trigger wich refreshes the report
And we do that on an interval we define in advance
I don’t want to do that. Thats called polling.
What I want is to just sit and wait and watch my report beeing refreshed.
And it’s all possible thanks to websockets and nodejs
So i did the change on the phone
Sent the change message to node
And node distributes the message to all the connected clients
Something like I explained you in the chat application
Well I promised to tell you more about the apex event trigger earlier on
Well I promised to tell you more about the apex event trigger earlier on.
When we receive a message from the node server we send out apex event trigger
The trigger fires on the document .
The task will be the event name.
And the payload will be the data part of the event.
We create a dynamic action on a custom event
The event name will be the task name of my json object
And as a selection type criteria we choose a javascript expression
The expression is the document, thats what we defined when we fired the event
And we can start creating some actions.
In our example I do a refresh
On GitHub you can find a lot of stuff
Facebook and Twitter are using GitHub
Oh yeah and also NASA
Over 10 Million repositories
One of them is the node-oracledb repository
One of them is the node-oracledb repository
You can download it from github and use the node package manager to install the package.
Then you can start building your own oracle connection in Node.
I use it in de Demo to store all the users in the oracle database.
First I include the oracledb package
Then I implement the insert user function
Therefore I first create a connection to the oracle database like this.
Then there is this callback function you use to implement your code when you are connected to the database
My function looks like this, you can copy this code if you want
If there is an error i just print it out to the console
Then i set the action and module name
You can use them from sqlplus to get some statistics.
V$SQL lists statistics on shared SQL area without the GROUP BY clause and contains one row for each child of the original SQL text entered. Statistics displayed in V$SQL are normally updated at the end of query execution. However, for long running queries, they are updated every 5 seconds. This makes it easy to see the impact of long running SQL statements while they are still in progress.
And this is the function wich really does the work
It merges the connected user to the user table
I first save the statement to a variable
And the execute the statement
And after that all the connected users are synced with the database and i can do anything with them
Like sending a specific message to one user.
If you want to organizing the public space and discuss overe where to put the playgrounds, streets and houses. You most probably are with a group of people discussing this.
Redlining refers to the process of adding MapNotes/symbols or drawing shapes to "mark up" GIS layers without actually editing the layers themselves.
If you want to organizing the public space and discuss overe where to put the playgrounds, streets and houses. You most probably are with a group of people discussing this.
Redlining refers to the process of adding MapNotes/symbols or drawing shapes to "mark up" GIS layers without actually editing the layers themselves.
If you want to organizing the public space and discuss overe where to put the playgrounds, streets and houses. You most probably are with a group of people discussing this.
Redlining refers to the process of adding MapNotes/symbols or drawing shapes to "mark up" GIS layers without actually editing the layers themselves.
If you want to organizing the public space and discuss overe where to put the playgrounds, streets and houses. You most probably are with a group of people discussing this.
Redlining refers to the process of adding MapNotes/symbols or drawing shapes to "mark up" GIS layers without actually editing the layers themselves.
I guess everybody requognise the next example.
You start a background process and you want to be informed when the job is finished
Well you can poll a lot in apex to check if the job is done.
But you can do it without polling
In the previous example we just received a message when the job was done
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
How nice would it be if we are informed from time to time
Until the job is complete
What we did here is make a rest request from the database to the node server
As you can see i use the same json object. You can define your own body if you’d like
But i’m sending the message right here: dbms_job_ready
With the job identifier in the payload
And on the http server i’ve created this function witch catches the rest request.
At first i parse the json string
And then do something with te data depending on the task
With dbms_job_ready i send the job ready event to the clients
And with the other message i sent some more information about the job status
And with the other message i sent some more information about the job status
And with the other message i sent some more information about the job status
And with the other message i sent some more information about the job status
And now we have just one demo to go.
Everywhere on the internet there are API’s available for different social networks
We have dropbox, google, twitter, facebook, linkedin
And many more
And they all have something in common. OAUTH 2
So when you want to connect to one of these api’s you have to use the oauth2 protocol to authenticate.
And after you are authenticated you can use a lot of api functions.
Just as the oracledb package, the dropbox package can be found on github
This was in unofficial, as the oracle db package was is made and maintained by oracle
In node js i first include the dropbox package
Then i authenticate agains a key, a secret and a token
And when i’mconnected i can use all kind of functions
Like here im using get accountinfo
And in this part a callback function handles the details
And you can put your details here. I have all the code working on my laptop. Just sent me an email an i can help you getting started.