Programming Tutorials

Event driven programming in node.js

By: William Alexander in node.js Tutorials on 2018-08-04  

In this tutorial we will see how node.js can be used to build an event driven application. It is assumed that you have node.js installed as well as an IDE to write and organize your code. If not it is highly recommended that you see this tutorial on how node.js can be used as a HTTP server which is an introduction to node.js and a getting started guide for node.js

What is event-driven programming?

In olden days when there was no GUI, the programs normally were run sequentially in a blocking manner. But with GUI, the event driven programming became popular as events were generated by the user in the form of mouse clicks. So the user has complete control of what to do next by navigating randomly within an application by invoking any menu or feature within the application. Each mouse click therefore is an event and based on that event a particular code will get executed. Hence in an event driven programming, there will be a main thread which listens to events and for each event there will be an event handler (code) that gets executed. Events therefore could be generated or triggered by an user interaction or by another event or by a hardware event etc..

Traditionally, server programs will use multi threading to handle concurrency. For example there will be a main loop which listens continuously for events to occur and for each event, there will be a dedicated child thread that handles the request by executing the event handler. Once the event handler is executed, the program returns back to the main loop. But each individual child thread is still blocking in the sense it will have to completely execute the I/O operations before exiting the thread.

Whereas in node.js, it is completely non-blocking because it uses JavaScript at the core. If you look at JavaScript's history, it was invented in 1995 to execute code within netscape browser on the client side. Because of this, JavaScript did not have any I/O capability as it just ran in client browser and cannot access a local file. And it was asynchronous and was created from the ground up as an event driven programming language, to handle events such as user clicking on a html form, or a page loading event etc. So when node.js developers decided to use JavaScript for node.js, they had to write the I/O libraries from scratch and they implemented a non-blocking I/O capability on the server side by dedicating a pool of thread that is dedicated to I/O.

So all you need to know is that node.js is single threaded, light weight, even-drivern, asynchronous, non-blocking server side application development platform. And it is perfect for event-driven programming applications.

node.js event driven programming

As discussed earlier, all events have to have an event handler and in node.js this is done using the package named 'events'. 

Create a file named 'eventdriven.js' and copy the below code:

var events = require('events');
var eventEmitter = new events.EventEmitter();

//Create an event handler:
var sampleEventHandler = function () {
  console.log('This is a sample event handler!');
    //You can write other code to execute when this event occurs
}

//Assign the event handler to an event:
eventEmitter.on('sampleevent', sampleEventHandler);

//Fire the 'sampleevent' event:
eventEmitter.emit('sampleevent');

Here we are just creating an event handler for the event 'sampleevent'. And an on function that maps the event to its event handler. And in our case the event handler just logs a message.

You can test this script by executing 'node eventdriven.js'







Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in node.js )

Latest Articles (in node.js)