Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
Read Email File
File Upload Container
 


  

 
Node.js Events
Question Posted on 12 Apr 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Events


Node.js Events

Node.js Events


When we say about the Node.js Events, in Node.js application events and callbacks are the concepts which are used to provide concurrency. Node.js applications are single threaded and every API of the Node.js are basicaly asynchronous. So to main concurrency here we uses async function. And node uses observer pattern. And Node.js thread keeps an event loop and after the completion of any tasks, it fires the corresponding event which will signals the event listener function to get executed.




Node.js Event Driven Programming


Well, Node.js uses event driven programming. Which means as soon as Node.js starts its server, this will simply initiates its variables, declares functions and then simply waits for event to occur. And this is one of the main reason that's why Node.js is pretty fast compared to other similar technologies.



There is a main loop in the event driven application which listens for events, and then triggers a callback function when of one of those events is detected.




Event Loop Node.js

Difference between Events and Callbacks:


We will define some most important difference in between Events and Callbacks. Although, Events and Callbacks look similar but the differenences lies in fact that callback functions are mainly called when an asynchronous function returns it result where as event handling works on the observer pattern. And whenever any of event get fired, which means listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which is used to bind events and event listeners.



EventEmitter class to bind event and event listener:

//Here we import events module

var events = require('events');

//Here we create an eventEmitter object

var eventEmitter = new events.EventEmitter();



To bind event handler with an event:

// Bind event and even handler as follows

eventEmitter.on('eventName', eventHandler);



To fire an event:
To bind event handler with an event:

// Fire an event

eventEmitter.emit('eventName');




Node.js Event Example


File: main.js

// Import events module

var events = require('events');

// Create an eventEmitter object

var eventEmitter = new events.EventEmitter();


// Create an event handler as follows

var connectHandler = function connected() {

console.log('connection succesful.');


// Fire the data_received event

eventEmitter.emit('data_received');

}


// Bind the connection event with the handler

eventEmitter.on('connection', connectHandler);

// Bind the data_received event with the anonymous function

eventEmitter.on('data_received', function(){

console.log('data received succesfully.');

});

// Fire the connection event

eventEmitter.emit('connection');

console.log("Program Ended.");





Now we open the Node.js command prompt and run the below code:


node main.js



Event Node.js





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |