Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
alespad
Contributor

Last year , I wrote 2 blogs about an integration between Html5 Websockets functionalties and Abap web application server through the Sap Code Exchange project Abap Pusher by patrizia.rossi2 and me.

A WebSocket is a web technology providing full-duplex communications channels over a single TCP connection,for more details you can check my old blog about it.

In a few words, Abap Pusher uses the http rest api of  Pusher.com Software As a Service,then our messages will be distributed from the pusher server to all the connected clients (pusher provides a lot of client libraries ).

This solution has a lot of benefits:

  • we don't need a server to manage websockets connections
  • a SAAS solution provides a lot of useful features ( monitoring, debug, apps management , autentication mechanism, etc )
  • a SAAS provides a lot of client libraries ( Java,Objective C,Javascript ,etc..)

A cloud service is a good solution but some people might say: "I don' t want to use a proprietary solution and a cloud service,

I want my own local server that supports websocket connections and realtime messages".

Ok,in these series of blog I will try to introduce the powerful of Node.js ,a perfect solution to build our simple websocket server application.

What is Node.js?

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. 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

nodejs.org

The revolution of Node.js is programming using Javascript from the Server side : it's very useful to create realtime applications,large file uploading and ,more in general, scalable network applications .

Node.js is fast because it runs on the Google V8 Javascript Engine

Node.js is event-driven (the asynchronous nature of Javascript)

when an operation is completed it triggers a callback function ( NON-BLOCKING ) , in Node.js there isn't a multi-thread concept but only one single thread.

a NON-BLOCKING example , Reading a File :

fs.readFile("./File.txt", function (err, content) {
     console.log("This will be logged after");
});
console.log("This will be logged first");

Installation

Go to http://nodejs.org/download/ and download it the right version for you platform (I'm using the Windows version)

To check if the installation was succesfull type 

node

at the command line and you'll be dropped into the node REPL ( Read-Eval-Print-Loop, http://nodejs.org/api/repl.html ) , inside REPL you can write any JavaScript  and it will be executed.

Now we are ready to write our first Node.js server.

Our first "Hello World"

var http = require('http'); //requiring a module
http.createServer(
  function (request, response)
  {
    response.writeHead(200);
    response.write('Hello World!');
    response.end();
  }).listen(8000);
console.log('Server running on port 8000');


Copy an paste inside a new js file HelloWorld.js

  and type 

node HelloWorld.js 

at the command line to start the server

Ok,our server is running on http://localhost:8000  , now open a browser and try to digit the url ,we'll see our server response with the "Hello World

In the part 2 I will show how to use the NPM Node Package Manager to installing  external modules such as Express

(probably the most populare Node web framework) and Socket.io to build our websocket node application.

All trademarks and registered trademarks are the property of their respective owners

2 Comments