After you install Node and our first "hello world" application described in Part 1 , we are ready to build our Node Server app to manage the websocket connections
Node Modules
The first line of code in HelloWorld.js was:
var http = require('http'); //requiring HTTP module (http.js)
It means we are requiring the HTTP library and HTTP.js file , for example we can define our own simple module like this :
myModule.js
var sum = function(a,b){
return a+b;
}
exports.sum = sum; //return sum function
and require it in App.js
var myModule = require('./myModule'); //requiring myModule.js
var result = myModule.sum(2,1);
console.log(result); //output 3
To returning a result of a "require call" we must use the exports object
npm is Node’s package manager. It maintains a registry of Node modules and allows one-line installation and version management of third-party packages. You can find modules in npm from the command line using npm search search term
Means, Garann. “Node for Front-End Developers.” O'Reilly Media, 2012
To build our application we need to require 2 modules :
- Express, the most popular web application framework for node
- Socket.io , "the cross-browser Websocket for realtime apps"
We can install a node Module locally or globally ,the node guideline says we should use a global install if the package
needs to be accessed on the command line .For our purpose we'll install both locally but if you want to learn more about local vs global check this blog
http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation
the command is : npm install module
so you need to digit from the command line npm install socket.io and
npm install express
to install them.
If everything is OK , we'll have a new folder "node_modules" containing all the folders modules
What is Socket.IO ?
Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.
socket.io
Socket.io is quite easy to use , it provides a server and a client libraries , the example described in socket.io homepage shows how it's simple to build a realtime app.
Server
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});});
Client
<script src="/socket.io/socket.io.js"></script><script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});</script>
The server for every connected client ( on 'connection' event) emits a news event with an "hello world" json object , the client listens on news event (triggered from the server) , it logs the "hello world" object and emits another event 'my other event' to the server. Is not so difficult , right? :wink:
Our websocket APP
How to trigger realtime events from ABAP?
The idea is simple..we can make from ABAP an HTTP request to the Node Express application (in our case , running on the same host of the SAP instance). The message will be pushed in realtime to all the connected clients.
The difference compared to my old experiments using pusher.com is that now we have the full control to the websocket server built with Node
This is our Server.js that we'll use to trigger messages from Abap to all the clients:
var express = require('express');
var io = require("socket.io").listen(1337); // create socket.io server on port 1337
function authorize(username, password) {
return 'SAP' === username & 'password' === password;
}
var app = express();
var auth = express.basicAuth(authorize);
app.configure(function () {
app.use(express.bodyParser());
});
app.get('/publish', auth, function (request, response) {
response.send("Server running"); //Ping for SM59 Test
});
app.post('/publish', auth, function (request, response) {
io.sockets.emit("SAP_Event", request.body);
console.log("SAP message:" + JSON.stringify(request.body));
response.send(200);
});
io.sockets.on('connection', function (client) {
console.log('Client connected...');
});
app.listen(7000); // Express /publish listening on 7000
console.log('Server Running...');
Once you create an Express instance with app = express() , it's possible to catch the Http Request using the Express "application routing mechanism" app.HttpVerb
A "basic Authentication" is required for all request to path /publish , from Abap we'll make an HTTP POST request to the URI with a JSON in Request Body, then the io.sockets.emit("SAP_Event",request.body) will trigger the JSON to all the connected clients
A simple Html/Javascript client listening for the "SAP_Event" messages is :
<script src="http://localhost:1337/socket.io/socket.io.js"></script>
<script>
var server = io.connect('http://localhost:1337');
server.on('SAP_Event', function (data) {
console.log(JSON.stringify(data));
});
</script>
Let's start the Server.js with node Server.js from the CMD
Opening a browser and executing the javascript/client side we can see that the client connection is successfully from the Server logs
Our websocket Node Server is running , in the part 3 we'll see how to trigger the messages from ABAP to the connected clients and I'll post a final video to see the "magic" of the realtime messages
All trademarks and registered trademarks are the property of their respective owners