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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in node.js ) |
Latest Articles (in node.js) |
Comments