

{"id":100667,"date":"2021-10-09T09:00:27","date_gmt":"2021-10-09T03:30:27","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100667"},"modified":"2021-10-09T10:36:17","modified_gmt":"2021-10-09T05:06:17","slug":"nodejs-event-emitter","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/","title":{"rendered":"Nodejs Event Emitter"},"content":{"rendered":"<p>Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. To use the functionalities of events like create, emit or listen we have to instantiate the eventemitter class. Let us learn about event emitter in nodejs.<\/p>\n<h3>Event-Driven programming:<\/h3>\n<p>When nodejs starts a server it initializes all its variables, declares functions, and keeps waiting for an event to happen. That is why node js applications are known as event-driven applications.<\/p>\n<h3>Event:<\/h3>\n<p>Event module in nodejs is a built-in module, this module contains the event emitter class which is used to implement event driven programming. To use this module in our file we use the require() function.<\/p>\n<p>Event emitter class generates the events in nodejs. The event emitter class and the javascript callbacks handles the event.<\/p>\n<h3>Event and its working:<\/h3>\n<p>Whenever any event occurs first the corresponding event handler is triggered. The call back function then executes, and the main loop keeps on listening for any new event triggers and calls the corresponding event handler.<\/p>\n<h3>Event Emitter and its working<\/h3>\n<p>Event Emitter is a class that is capable of emitting an event, attaching an event and also detaching it.<\/p>\n<p>Https and other node js core modules use the event emitter class.<\/p>\n<p>The event emitter class calls all the events in the same order in which they were registered.<\/p>\n<p>The on() method is used to attach the event handler, this method takes a callback function that gets attached to the event. More than one function can also be attached to a single event.<\/p>\n<p>The emit() method is used to emit the attached event, basically, it invokes the attached function.<\/p>\n<p><strong>Code for event emitter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1() {\r\n    console.log('First event occurred!')\r\n}\r\n \r\nfunction event2() {\r\n    console.log('First event occurred!')\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.on('someevent', event2)\r\nemitter.emit('someevent')\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100890\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method.png\" alt=\"node.js emit method\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-emit-method-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>In the above image, you can see that the function event1 and event 2 has been registered by the name \u201csomeevent\u201d using the on() method.<\/p>\n<p>Next, we used the emit method to fire the event and we can see the output of both the functions which means that both the functions were invoked as soon as the \u201csomeevent\u201d event was fired.<\/p>\n<h3>Emitting event with parameter.<\/h3>\n<p>We can also pass data to the call-back function. In order to do so, we have to pass the data in the second argument of emit() method.<\/p>\n<p><strong>Code for event emitter with parameter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100891\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener.png\" alt=\"node.js addlistener\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that we have passed a name with value \u201cDataFlair\u201d in our second argument. This value is then used by the event1 function.<\/p>\n<h3>Event Emitter Methods:<\/h3>\n<p>Below are the methods present in the event emitter with explanation and code.<\/p>\n<h4>Detach Event;<\/h4>\n<p>We can detach the previously attached event by using the off() method.<\/p>\n<p><strong>Code for detaching event:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100892\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method.png\" alt=\"node.js detach method\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-detach-method-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that we have used emit method twice to emit the same event but the output came only for that emit which was called before detaching the event using the off() method.<\/p>\n<p>So once you detach an event then the emit method does not have any effect for that event.<\/p>\n<h4>Count Listener:<\/h4>\n<p>listenerCount() method returns the number of listeners for that event.<\/p>\n<p>You can see in the below image that listenerCount() return 1 at first than as we called the off() method the listener for the event is detached and hence the count of listener is now zero<\/p>\n<p><strong>Code for count listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.listenerCount('someevent'))\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.listenerCount('someevent'))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100893\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count.png\" alt=\"node.js listener count\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-listener-count-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>Raw Listener:<\/h4>\n<p>It returns an array of listeners for that particular event.<\/p>\n<p><strong>Code for raw listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.on('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.rawListeners('someevent'))\r\nemitter.off('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nconsole.log(emitter.rawListeners('someevent'))\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100894\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener.png\" alt=\"node.js raw listener\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-raw-listener-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>Before calling the off() method there was one listener so it returned its type and name. After the off() method was called nothing was attached to the event so it returned an empty array.<\/p>\n<h4>Add Listener:<\/h4>\n<p>This is similar to the on method. It takes the event name and a callback function and then attaches the event. It checks the event array to see if the event has already been attached or not. If the event is attached it returns the array, else return empty array.<\/p>\n<p><strong>Code for adding listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.addListener('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100895\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1.png\" alt=\"node.js addlistener\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-addlistener-1-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that the result is same as the on() method<\/p>\n<h4>Remove Listener:<\/h4>\n<p>It is similar to the off() method. It checks in the event array for the event and it removes it from the event array.<\/p>\n<p><strong>Code for removing listener:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\n \r\nfunction event1(arg) {\r\n    console.log('Welcome to', arg.name);\r\n}\r\n \r\nemitter.addListener('someevent', event1)\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\nemitter.removeListener('someevent', event1);\r\nemitter.emit('someevent', { name: \"DataFlair\" })\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100896\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener.png\" alt=\"node.js removelistener\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-removelistener-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>The answer is the same as that for the off() method.<\/p>\n<h4>Once:<\/h4>\n<p>If we want an event to be used only one time then we can use the once() method. It takes eventname and a callback function.<\/p>\n<p>When the event is first emitted it invokes the callback function and after that it detaches the event automatically, after that It will not respond to any more emitted calls.<\/p>\n<p><strong>Code for once:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const EventEmitter = require('events');\r\nconst emitter = new EventEmitter();\r\nfunction event1() {\r\n    console.log('Welcome to DataFlair');\r\n}\r\nemitter.once('someevent', event1)\r\nemitter.emit('someevent')\r\nemitter.emit('someevent')\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100897\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter.png\" alt=\"node.js once emitter\" width=\"1366\" height=\"723\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter-768x406.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter-720x381.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter-520x275.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/node.js-once-emitter-320x169.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>You can see that the once method invoked the callback function only when the first emit() method executed, the next emit method did not invoke the callback function as the event already got detached.<\/p>\n<h3>Summary<\/h3>\n<p>In this article, we have seen the builtin event module of node js and its methods. We hope you enjoyed the article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. To use the functionalities of events like create, emit or listen we have to instantiate&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":100889,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25271,25272],"class_list":["post-100667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-event-emitter","tag-nodejs-event-emitter-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Event Emitter - DataFlair<\/title>\n<meta name=\"description\" content=\"Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. Learn about event emitter in Nodejs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Event Emitter - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. Learn about event emitter in Nodejs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-09T03:30:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-09T05:06:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nodejs Event Emitter - DataFlair","description":"Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. Learn about event emitter in Nodejs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Event Emitter - DataFlair","og_description":"Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. Learn about event emitter in Nodejs.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-10-09T03:30:27+00:00","article_modified_time":"2021-10-09T05:06:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Event Emitter","datePublished":"2021-10-09T03:30:27+00:00","dateModified":"2021-10-09T05:06:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/"},"wordCount":825,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg","keywords":["Nodejs Event Emitter","Nodejs Event Emitter methods"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/","name":"Nodejs Event Emitter - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg","datePublished":"2021-10-09T03:30:27+00:00","dateModified":"2021-10-09T05:06:17+00:00","description":"Events is a built-in module in node.js. In order to include it you have to use the \u201crequire\u201d function. Learn about event emitter in Nodejs.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-events.jpg","width":1200,"height":628,"caption":"nodejs event emitter"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-event-emitter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Event Emitter"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=100667"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100667\/revisions"}],"predecessor-version":[{"id":100898,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100667\/revisions\/100898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/100889"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}