Events and EventEmitter 

Many objects in Node emit events: a net.Server emits an event each time a peer connects to it, a fs.readStream emits an event when the file is opened. All objects which emit events are instances of events.EventEmitter. You can access this module by doing: require("events");

Typically, event names are represented by a camel-cased string, however, there aren't any strict restrictions on that, as any string will be accepted.

Functions can then be attached to objects, to be executed when an event is emitted. These functions are called listeners. Inside a listener function, this refers to the EventEmitter that the listener was attached to.

 

To access the EventEmitter class, require('events').EventEmitter.

When an EventEmitter instance experiences an error, the typical action is to emit an 'error' event. Error events are treated as a special case in node. If there is no listener for it, then the default action is to print a stack trace and exit the program.

All EventEmitters emit the event 'newListener' when new listeners are added and 'removeListener' when a listener is removed.

These methods are used to register behaviors to take effect when the user interacts, and to further manipulate those registered behaviors. The emit function may be called asynchronously, but note that all the listener functions will be executed synchronously, in the order they were added, before any execution can continue in statements following the call to emit.

THE DOM triggers Events. You can listen for those events. Many Objects in Node emit Events

 

Example Basic Event using EventEmitter 

var EventEmitter = require('events').EventEmitter;
var logger = new EventEmitter(); // error, warn, info

example : listen for error event

logger.on('error',function(message){
cosole.log('ERR: ' + message);
});
logger.emit('error', 'Spilled Milk');

BREAKING IT DOWN

http.createServer( function(request,response) {}); 
http.createServer([requestListener]); // return a new web server object

The requestListener is a function which is automatically added to the 'request' event using Class http.Server

This is an EventEmitter with the following events:
Event:'request'

function (request, response) {}

Emitted each time there is a request

ALTERNATE SYNTAX

http.createServer( function(request,response) {});

same as

var server = http.createServer();
server.on('request', function(request,response){ ... });


This is how we add event listeners

Event: 'close'
Emitted when the server closes.

server.on('close', function(){ ... });



Example Event chat

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

var chat = new EventEmitter();
var users = [], chatlog = [];

chat.on('message', function(message) {
chatlog.push(message);
});

chat.on('join', function(nickname) {
users.push(nickname);
});

// Emit events here
chat.emit('join', 'sarah');
chat.emit('message', 'hi i am sarah');


Example 2 Event http server

var http = require('http');
var server = http.createServer();

server.on('request', function(request, response) {
response.writeHead(200);
response.write("Hello, this is dog");
response.end();
});

server.on('request', function(request, response) {
console.log("New request coming in...");
});

server.on('close', function() {
console.log("Closing down the server...");
});

server.listen(8080);

 

Leave a Comment:



digital_ocean