There are a few features of Node.JS which in some way set it apart from other frameworks i.e. Standard Library , Module System and npm (Node Package Manager).

The Standard library is by far Node’s most powerful feature and consists primarily of two parts; A set of Binary libraries and the core modules. The binary libraries include libuv , which provides a fast run loop and non-blocking I/O for networking and the file system.

Node’s core modules are mostly written in JavaScript. This includes features like networking, high-level file system operations, the module system, and streams. It also includes Node-specific features like running multiple Node processes at once with the cluster module, and wrapping sections of code in event-based error handlers, known as domains.

Exploring Node’s core modules like streams, networking and File system API’s, you’ll notice that most classes derive from the EventEmitter class, and for the most part developers are able to inherit from EventEmitter to develop their event-based API’s.

events are core concept within nodeJS, is which is primarily to do with the fact that the whole driving ethos for node is event driven architecture.

The web is slowly transforming from a big event model to micro events model.

big event model

A standard web form application can be defined as a “big event” submitter. Usually the user fills in a web form, in a typical data capture scenario there may be multiple text boxes, select lists, combo boxes , option buttons the user completes and fires off the submit button.

The data is then sent to the server via a POST in a “single big event” from a programming perspective. This was model most developers employed in the pre AJAX era.

Micro Event Model

The introduction of AJAX came what has become known as evented programming . Culminating in more events triggering interaction with the server. This typically enabled validating the data in controls as the user moved focus from the control, which guided the user in completing the data correctly before firing off the big event again to submit the data to the server.

AJAX was used to create interesting visual effects , quick validations and submit forms without having to navigate away from the page, than it was to create truly event driven web pages.

Every time a request is sent – irrespective of size – causes an increase in network traffic. A server has to respond to that request, usually with a new process in its own thread. Therefore if you really moved to an event driven architecture , where one might have 10 – 15 micro requests going from a single page to a server , you could be triggering in the region of 10 – 15 threads , depending on thread pooling and reclaimation strategies employed. Scaling this model up to potentially 1 000, 10 000 or even a 100 000 copies of the same page been triggered at a time you would experience total network chaos. The side effects experience will be network slowdown and system crashes.




Sending a little data at all times

Node enables a different approach and strategy. A truly event driven architecture , with the introduction of tons of requests, on lots of events, with tiny bits of data, or requests that need a response with only a tiny bit of data.

Whats the difference ?

Node’s goal is to provide an easy way to build scalable network programs. Node tells the operating system (through epoll, kqueue, /dev/poll or select) that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation.

So essentially node has no blocks, no threads competing for resource, nothing has to happen on start up upon request. Node just sits around waiting and when a request comes in it’s handled. This results in very fast code!

How to avoid chaos

It is worth noting that this model is not without it’s problems, and you still need to have a clear understanding of how the event driven architecture works and what exactly constitutes an event and how to implement events in node.js.

What is an event?

Events enable an object to notify other objects when something of interest occurs. The object that sends (or raises) the event is called the publisher and the objects that receive (or handle) the event are called subscribers.

Events Module

Node.js provides an events module to manipulate communication based on an event. For further information this module, you can visit the events module document on http://nodejs.org/api/events.html

In this example I will use the EventEmitter object to create events.

  • First, we load the events module.
  • Define the EventEmitter object and instantiate it.
  • We can define a function variable or put the function into the on() method directly.
  • To send the message, we can use the emit() method with the event name and data as parameters.




Executing the application will result in the following displayed in the console:

 Once event listener

Making use of the on() method,  results in the  event listener listening  for the event forever until the application closes.  If you need to ensure that the event is only executed and listened for once, you can use the once() method.

Remove Listener

A situation may arise later in your application where you may want to remove an event in your code.  You can do this by calling the removeListner function

Although not an exhaustive example this does provide some simple examples of how to use events in node.js . I do plan to post more illustrative examples of how to program events in node.js , I will be adding links to the examples here as they are published.

 

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines. A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

π