Scaling web applications to support common web usage required the support of hundreds of ongoing connections between the server and browser. Most web platforms used expensive threads to handle requests, which meant keeping a fair amount of idle threads in order to keep the connection alive. This requires using non-blocking sockets in order to save system resources.
What is Node.JS
The initial release of NodeJS framework made use of V8 Engine as a wrapper around solid C code implementing non-blocking sockets and released to the community for review. After an initial warm response it was further expanded and enhanced.
Node.JS was required to run in a server environment therefore required certain extensions which made sense in a server context i.e. Access to the file sytem.
What makes Node.JS unique
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.
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
AJAX 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 , quickly validating 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 approximately 10 – 15 threads , depending on thread pooling and re-reclamation 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 , introducing 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.
sharing of a single global namespace.
3 key components CommonJS standards
require()Used to load modules into code
exportsExposes code when module is loaded
metadataabout the module and pointers of an
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
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
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.
Event-Driven programming is a core concept behind node.js which is manifested by the implementation of the
Events module. The events module is used to Create, Emit and Handle events.
The event loop is an entry point used to trigger an event that invokes a corresponding event handler which in turn can invoke further events resulting in the event driven programming.
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.