In my last post we discussed how nodejs can be used to create a http server, but it had some practical problems associated with it. Our ready to deploy server can listen to incoming request on designated port, but it simply didn’t know what to do next ! We discussed the need of routing handlers. We also left the topic of ” asynchronous programming” to be covered later.
My so far experience with nodejs tells me that “asynchronous programming” is the deal, it is what that differentiates node from everything else, so how do we explain that ?. Lets go back to elementary school. Sure we all do have had hands on programming languages like “BASIC” or event a little of batch programming on windows. A simple “Hello World” would in BASIC would be written like this
1 2 3 4 |
10 START 20 PRINT "Hello World !" 30 RUN 40 STOP |
Poor BASIC everybody forgot you !, It was surely the coolest stuff when I was just a 5th grader. Of course this is stupidity now, you decide the flow of your program, and external influence doesn’t really influence the flow, things went on like this with many other programming languages and we heard a new concepts of modular programming and OOPS. Sure these are cool concepts but you’re still deciding the flow ! It’s just that external influence do influences your flow, but then lets say, you’ve heavy database driven application and somewhere in your code you’ve this snippet:
1 2 3 4 |
doDatabaseAction(); doSomeAction(); doFileAction(); doAnotherAction(); |
What if all these individual modules are independent of each other ? Database and File I/O are blocking process, that means doSomeAction() and doAnotherAction() will have to wait untill Database and File I/O perform and release the control ! This form of programming is called Synchronous Programming, on the other hand NodeJS adapts Asynchronous Programming methdology, which means every module ( technically its called an event, we’ll just call it a module for simplicity and analogy ) has this unique feature of “callback“, its says ” Okei guys I’m doing something, whenever I’ll be finish with my operation, I’ll do the callback, in the meanwhile you can continue your normal routine, I won’t block it”
How do we illustrate this ? lets go back to our Simple HTTP Server that we created
1 2 |
var app = http.createServer(function(req,res){ res.writeHead(200,{"Content-Type": "text/plain"}); res.end("Welcome to JellyFish Technologies"); |
Here, we actually wait for an incoming connection, and whenever we get a connect we simply make a callback to function(req,res) which renders content to our page.
About Events and listeners
The very reason that nodejs is faster is because it is based around events and asynchronous programming. Instead of initiating everything to process even 1% of your code ( like PHP ) it simply creates a server, initiates a few necessary things and wait ! It simply waits for an event to occur, there is a totally separate handle called “event-loop” to handle events listening and handling them. We write relevant event listeners for possible events that our program may receive. Our event-loop works independently, and whenever an event occurs it simply initiates the required listener, how stupendous that is !
Nodejs comes with powerful library to deal with events, we can create our own events, listen to them and handle them. Lets just start by emitting an event, for this we require a core module “events” and the EventEmitter() method. Let’s start by emitting an event of knocking on the door.
var
events = require(
'events'
);
var
eventEmitter =
new
events.EventEmitter();
var
knockDoor =
function
knockedMyDoor(){
console.log(
'Opening the Door'
);}
eventEmitter.on(
'knockMyDoor'
, knockDoor);
eventEmitter.emit(
'knockMyDoor'
);
Whenever we’ll emit knockMyDoor, our event handler will call knockedMyDoor to open the door ! beside eventEmitter.on() there are few other methods which help us in listening to events.
We can create multiple handlers to a single event like
1 2 |
eventEmitter.on('knockMyDoor', something()); eventEmitter.on('knockMyDoor', doSomethingElse()); |
Recent Comments