{
  "source": "doc/api/events.markdown",
  "modules": [
    {
      "textRaw": "Events",
      "name": "Events",
      "stability": 2,
      "stabilityText": "Stable",
      "type": "module",
      "desc": "<p>Many objects in Node.js emit events: a <code>net.Server</code> emits an event each time\na peer connects to it, a <code>fs.readStream</code> emits an event when the file is\nopened. All objects which emit events are instances of <code>events.EventEmitter</code>.\nYou can access this module by doing: <code>require(&quot;events&quot;);</code>\n\n</p>\n<p>Typically, event names are represented by a camel-cased string, however,\nthere aren&#39;t any strict restrictions on that, as any string will be accepted.\n\n</p>\n<p>Functions can then be attached to objects, to be executed when an event\nis emitted. These functions are called <em>listeners</em>. Inside a listener\nfunction, <code>this</code> refers to the <code>EventEmitter</code> that the listener was\nattached to.\n\n\n</p>\n",
      "classes": [
        {
          "textRaw": "Class: events.EventEmitter",
          "type": "class",
          "name": "events.EventEmitter",
          "desc": "<p>Use <code>require(&#39;events&#39;)</code> to access the EventEmitter class.\n\n</p>\n<pre><code class=\"javascript\">var EventEmitter = require(&#39;events&#39;);</code></pre>\n<p>When an <code>EventEmitter</code> instance experiences an error, the typical action is\nto emit an <code>&#39;error&#39;</code> event.  Error events are treated as a special case in\nNode.js.  If there is no listener for it, then the default action is to print\na stack trace and exit the program.\n\n</p>\n<p>All EventEmitters emit the event <code>&#39;newListener&#39;</code> when new listeners are\nadded and <code>&#39;removeListener&#39;</code> when a listener is removed.\n\n</p>\n",
          "methods": [
            {
              "textRaw": "emitter.addListener(event, listener)",
              "type": "method",
              "name": "addListener",
              "desc": "<p>Adds a listener to the end of the listeners array for the specified <code>event</code>.\nNo checks are made to see if the <code>listener</code> has already been added. Multiple\ncalls passing the same combination of <code>event</code> and <code>listener</code> will result in the\n<code>listener</code> being added multiple times.\n\n</p>\n<pre><code>server.on(&#39;connection&#39;, function (stream) {\n  console.log(&#39;someone connected!&#39;);\n});</code></pre>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "listener"
                    }
                  ]
                },
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "listener"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.on(event, listener)",
              "type": "method",
              "name": "on",
              "desc": "<p>Adds a listener to the end of the listeners array for the specified <code>event</code>.\nNo checks are made to see if the <code>listener</code> has already been added. Multiple\ncalls passing the same combination of <code>event</code> and <code>listener</code> will result in the\n<code>listener</code> being added multiple times.\n\n</p>\n<pre><code>server.on(&#39;connection&#39;, function (stream) {\n  console.log(&#39;someone connected!&#39;);\n});</code></pre>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "listener"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.once(event, listener)",
              "type": "method",
              "name": "once",
              "desc": "<p>Adds a <strong>one time</strong> listener for the event. This listener is\ninvoked only the next time the event is fired, after which\nit is removed.\n\n</p>\n<pre><code>server.once(&#39;connection&#39;, function (stream) {\n  console.log(&#39;Ah, we have our first user!&#39;);\n});</code></pre>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "listener"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.removeListener(event, listener)",
              "type": "method",
              "name": "removeListener",
              "desc": "<p>Remove a listener from the listener array for the specified event.\n<strong>Caution</strong>: changes array indices in the listener array behind the listener.\n\n</p>\n<pre><code>var callback = function(stream) {\n  console.log(&#39;someone connected!&#39;);\n};\nserver.on(&#39;connection&#39;, callback);\n// ...\nserver.removeListener(&#39;connection&#39;, callback);</code></pre>\n<p><code>removeListener</code> will remove, at most, one instance of a listener from the\nlistener array. If any single listener has been added multiple times to the\nlistener array for the specified <code>event</code>, then <code>removeListener</code> must be called\nmultiple times to remove each instance.\n\n</p>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "listener"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.removeAllListeners([event])",
              "type": "method",
              "name": "removeAllListeners",
              "desc": "<p>Removes all listeners, or those of the specified event. It&#39;s not a good idea to\nremove listeners that were added elsewhere in the code, especially when it&#39;s on\nan emitter that you didn&#39;t create (e.g. sockets or file streams).\n\n</p>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event",
                      "optional": true
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.setMaxListeners(n)",
              "type": "method",
              "name": "setMaxListeners",
              "desc": "<p>By default EventEmitters will print a warning if more than 10 listeners are\nadded for a particular event. This is a useful default which helps finding\nmemory leaks. Obviously not all Emitters should be limited to 10. This function\nallows that to be increased. Set to zero for unlimited.\n\n</p>\n<p>Returns emitter, so calls can be chained.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "n"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.getMaxListeners()",
              "type": "method",
              "name": "getMaxListeners",
              "desc": "<p>Returns the current max listener value for the emitter which is either set by\n<code>emitter.setMaxListeners(n)</code> or defaults to <code>EventEmitter.defaultMaxListeners</code>.\n\n</p>\n<p>This can be useful to increment/decrement max listeners to avoid the warning\nwhile not being irresponsible and setting a too big number.\n\n</p>\n<pre><code>emitter.setMaxListeners(emitter.getMaxListeners() + 1);\nemitter.once(&#39;event&#39;, function () {\n  // do stuff\n  emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));\n});</code></pre>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "emitter.listeners(event)",
              "type": "method",
              "name": "listeners",
              "desc": "<p>Returns a copy of the array of listeners for the specified event.\n\n</p>\n<pre><code>server.on(&#39;connection&#39;, function (stream) {\n  console.log(&#39;someone connected!&#39;);\n});\nconsole.log(util.inspect(server.listeners(&#39;connection&#39;))); // [ [Function] ]</code></pre>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.emit(event[, arg1][, arg2][, ...])",
              "type": "method",
              "name": "emit",
              "desc": "<p>Execute each of the listeners in order with the supplied arguments.\n\n</p>\n<p>Returns <code>true</code> if event had listeners, <code>false</code> otherwise.\n\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "event"
                    },
                    {
                      "name": "arg1",
                      "optional": true
                    },
                    {
                      "name": "arg2",
                      "optional": true
                    },
                    {
                      "name": "...",
                      "optional": true
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "emitter.listenerCount(type)",
              "type": "method",
              "name": "listenerCount",
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`type` {Value} The type of event ",
                      "name": "type",
                      "type": "Value",
                      "desc": "The type of event"
                    }
                  ]
                },
                {
                  "params": [
                    {
                      "name": "type"
                    }
                  ]
                }
              ],
              "desc": "<p>Returns the number of listeners listening to the <code>type</code> of event.\n\n</p>\n"
            }
          ],
          "properties": [
            {
              "textRaw": "EventEmitter.defaultMaxListeners",
              "name": "defaultMaxListeners",
              "desc": "<p><code>emitter.setMaxListeners(n)</code> sets the maximum on a per-instance basis.\nThis class property lets you set it for <em>all</em> <code>EventEmitter</code> instances,\ncurrent and future, effective immediately. Use with care.\n\n</p>\n<p>Note that <code>emitter.setMaxListeners(n)</code> still has precedence over\n<code>EventEmitter.defaultMaxListeners</code>.\n\n\n</p>\n"
            }
          ],
          "classMethods": [
            {
              "textRaw": "Class Method: EventEmitter.listenerCount(emitter, event)",
              "type": "classMethod",
              "name": "listenerCount",
              "stability": 0,
              "stabilityText": "Deprecated: Use [emitter.listenerCount][] instead.",
              "desc": "<p>Returns the number of listeners for a given event.\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "emitter"
                    },
                    {
                      "name": "event"
                    }
                  ]
                }
              ]
            }
          ],
          "events": [
            {
              "textRaw": "Event: 'newListener'",
              "type": "event",
              "name": "newListener",
              "params": [],
              "desc": "<p>This event is emitted <em>before</em> a listener is added. When this event is\ntriggered, the listener has not been added to the array of listeners for the\n<code>event</code>. Any listeners added to the event <code>name</code> in the newListener event\ncallback will be added <em>before</em> the listener that is in the process of being\nadded.\n\n\n</p>\n"
            },
            {
              "textRaw": "Event: 'removeListener'",
              "type": "event",
              "name": "removeListener",
              "params": [],
              "desc": "<p>This event is emitted <em>after</em> a listener is removed.  When this event is\ntriggered, the listener has been removed from the array of listeners for the\n<code>event</code>.\n\n</p>\n"
            }
          ],
          "modules": [
            {
              "textRaw": "Inheriting from 'EventEmitter'",
              "name": "inheriting_from_'eventemitter'",
              "desc": "<p>Inheriting from <code>EventEmitter</code> is no different from inheriting from any other\nconstructor function. For example:\n\n</p>\n<pre><code>&#39;use strict&#39;;\nconst util = require(&#39;util&#39;);\nconst EventEmitter = require(&#39;events&#39;).EventEmitter;\n\nfunction MyEventEmitter() {\n  // Initialize necessary properties from `EventEmitter` in this instance\n  EventEmitter.call(this);\n}\n\n// Inherit functions from `EventEmitter`&#39;s prototype\nutil.inherits(MyEventEmitter, EventEmitter);</code></pre>\n",
              "type": "module",
              "displayName": "Inheriting from 'EventEmitter'"
            }
          ]
        }
      ]
    }
  ]
}
