UNPKG

learnyounode

Version:

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.

270 lines (236 loc) 13.1 kB
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Events Node.js v0.10.35 Manual &amp; Documentation</title> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/sh.css"> <link rel="canonical" href="http://nodejs.org/api/events.html"> <script type="text/javascript" src="//use.typekit.net/mse5tqx.js"></script> <script type="text/javascript">try{Typekit.load();}catch(e){}</script> </head> <body class="alt apidoc int docs" id="docs"> <div id="nav"> <img id="logo" src="assets/logo.svg" alt="node.js"> <ul> <li><a href="http://nodejs.org">Home</a></li> <li><a href="http://nodejs.org/download/">Downloads</a></li> <li class="active"><a href="http://nodejs.org/documentation/">Docs</a></li> <li><a href="http://nodejs.org/community/">Community</a></li> <li><a href="http://nodejs.org/about/">About</a></li> <li><a href="http://jobs.nodejs.org">Jobs</a></li> <li><a href="http://blog.nodejs.org">Blog</a></li> </ul> </div> <div id="content-wrap"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <!--<img src="/images/logo-light.svg" alt="node.js" width="170">--> <ul class="docs-nav"> <li><a href="http://nodejs.org/documentation/">About Docs</a></li> <li><a href="http://nodejs.org/documentation/tutorials/">Tutorials</a></li> <li><a href="http://nodejs.org/documentation/contributing/">Contributing</a></li> <li><a href="http://nodejs.org/documentation/localization/">Localization</a></li> <li class="active"><a href="http://nodejs.org/api/">API Docs</a></li> </ul> </div> <div id="column1" class="interior"> <header> <h1>Node.js v0.10.35 Manual &amp; Documentation</h1> <div id="gtoc"> <p> <a href="index.html" name="toc">Index</a> | <a href="all.html">View on single page</a> | <a href="events.json">View as JSON</a> </p> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li><a href="#events_events">Events</a><ul> <li><a href="#events_class_events_eventemitter">Class: events.EventEmitter</a><ul> <li><a href="#events_emitter_addlistener_event_listener">emitter.addListener(event, listener)</a></li> <li><a href="#events_emitter_on_event_listener">emitter.on(event, listener)</a></li> <li><a href="#events_emitter_once_event_listener">emitter.once(event, listener)</a></li> <li><a href="#events_emitter_removelistener_event_listener">emitter.removeListener(event, listener)</a></li> <li><a href="#events_emitter_removealllisteners_event">emitter.removeAllListeners([event])</a></li> <li><a href="#events_emitter_setmaxlisteners_n">emitter.setMaxListeners(n)</a></li> <li><a href="#events_emitter_listeners_event">emitter.listeners(event)</a></li> <li><a href="#events_emitter_emit_event_arg1_arg2">emitter.emit(event, [arg1], [arg2], [...])</a></li> <li><a href="#events_class_method_eventemitter_listenercount_emitter_event">Class Method: EventEmitter.listenerCount(emitter, event)</a></li> <li><a href="#events_event_newlistener">Event: &#39;newListener&#39;</a></li> <li><a href="#events_event_removelistener">Event: &#39;removeListener&#39;</a></li> </ul> </li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>Events<span><a class="mark" href="#events_events" id="events_events">#</a></span></h1> <pre class="api_stability_4">Stability: 4 - API Frozen</pre><!--type=module--> <p>Many objects in Node emit events: a <code>net.Server</code> emits an event each time a peer connects to it, a <code>fs.readStream</code> emits an event when the file is opened. All objects which emit events are instances of <code>events.EventEmitter</code>. You can access this module by doing: <code>require(&quot;events&quot;);</code> </p> <p>Typically, event names are represented by a camel-cased string, however, there aren&#39;t any strict restrictions on that, as any string will be accepted. </p> <p>Functions can then be attached to objects, to be executed when an event is emitted. These functions are called <em>listeners</em>. Inside a listener function, <code>this</code> refers to the <code>EventEmitter</code> that the listener was attached to. </p> <h2>Class: events.EventEmitter<span><a class="mark" href="#events_class_events_eventemitter" id="events_class_events_eventemitter">#</a></span></h2> <p>To access the EventEmitter class, <code>require(&#39;events&#39;).EventEmitter</code>. </p> <p>When an <code>EventEmitter</code> instance experiences an error, the typical action is to emit an <code>&#39;error&#39;</code> 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. </p> <p>All EventEmitters emit the event <code>&#39;newListener&#39;</code> when new listeners are added and <code>&#39;removeListener&#39;</code> when a listener is removed. </p> <h3>emitter.addListener(event, listener)<span><a class="mark" href="#events_emitter_addlistener_event_listener" id="events_emitter_addlistener_event_listener">#</a></span></h3> <h3>emitter.on(event, listener)<span><a class="mark" href="#events_emitter_on_event_listener" id="events_emitter_on_event_listener">#</a></span></h3> <p>Adds a listener to the end of the listeners array for the specified <code>event</code>. No checks are made to see if the <code>listener</code> has already been added. Multiple calls passing the same combination of <code>event</code> and <code>listener</code> will result in the <code>listener</code> being added multiple times. </p> <pre><code>server.on(&#39;connection&#39;, function (stream) { console.log(&#39;someone connected!&#39;); });</code></pre> <p>Returns emitter, so calls can be chained. </p> <h3>emitter.once(event, listener)<span><a class="mark" href="#events_emitter_once_event_listener" id="events_emitter_once_event_listener">#</a></span></h3> <p>Adds a <strong>one time</strong> listener for the event. This listener is invoked only the next time the event is fired, after which it is removed. </p> <pre><code>server.once(&#39;connection&#39;, function (stream) { console.log(&#39;Ah, we have our first user!&#39;); });</code></pre> <p>Returns emitter, so calls can be chained. </p> <h3>emitter.removeListener(event, listener)<span><a class="mark" href="#events_emitter_removelistener_event_listener" id="events_emitter_removelistener_event_listener">#</a></span></h3> <p>Remove a listener from the listener array for the specified event. <strong>Caution</strong>: changes array indices in the listener array behind the listener. </p> <pre><code>var callback = function(stream) { console.log(&#39;someone connected!&#39;); }; server.on(&#39;connection&#39;, callback); // ... server.removeListener(&#39;connection&#39;, callback);</code></pre> <p><code>removeListener</code> will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified <code>event</code>, then <code>removeListener</code> must be called multiple times to remove each instance. </p> <p>Returns emitter, so calls can be chained. </p> <h3>emitter.removeAllListeners([event])<span><a class="mark" href="#events_emitter_removealllisteners_event" id="events_emitter_removealllisteners_event">#</a></span></h3> <p>Removes all listeners, or those of the specified event. It&#39;s not a good idea to remove listeners that were added elsewhere in the code, especially when it&#39;s on an emitter that you didn&#39;t create (e.g. sockets or file streams). </p> <p>Returns emitter, so calls can be chained. </p> <h3>emitter.setMaxListeners(n)<span><a class="mark" href="#events_emitter_setmaxlisteners_n" id="events_emitter_setmaxlisteners_n">#</a></span></h3> <p>By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited. </p> <h3>emitter.listeners(event)<span><a class="mark" href="#events_emitter_listeners_event" id="events_emitter_listeners_event">#</a></span></h3> <p>Returns an array of listeners for the specified event. </p> <pre><code>server.on(&#39;connection&#39;, function (stream) { console.log(&#39;someone connected!&#39;); }); console.log(util.inspect(server.listeners(&#39;connection&#39;))); // [ [Function] ]</code></pre> <h3>emitter.emit(event, [arg1], [arg2], [...])<span><a class="mark" href="#events_emitter_emit_event_arg1_arg2" id="events_emitter_emit_event_arg1_arg2">#</a></span></h3> <p>Execute each of the listeners in order with the supplied arguments. </p> <p>Returns <code>true</code> if event had listeners, <code>false</code> otherwise. </p> <h3>Class Method: EventEmitter.listenerCount(emitter, event)<span><a class="mark" href="#events_class_method_eventemitter_listenercount_emitter_event" id="events_class_method_eventemitter_listenercount_emitter_event">#</a></span></h3> <p>Return the number of listeners for a given event. </p> <h3>Event: &#39;newListener&#39;<span><a class="mark" href="#events_event_newlistener" id="events_event_newlistener">#</a></span></h3> <div class="signature"><ul> <li><code>event</code> <span class="type">String</span> The event name</li> <li><code>listener</code> <span class="type">Function</span> The event handler function</li> </div></ul> <p>This event is emitted any time a listener is added. When this event is triggered, the listener may not yet have been added to the array of listeners for the <code>event</code>. </p> <h3>Event: &#39;removeListener&#39;<span><a class="mark" href="#events_event_removelistener" id="events_event_removelistener">#</a></span></h3> <div class="signature"><ul> <li><code>event</code> <span class="type">String</span> The event name</li> <li><code>listener</code> <span class="type">Function</span> The event handler function</li> </div></ul> <p>This event is emitted any time someone removes a listener. When this event is triggered, the listener may not yet have been removed from the array of listeners for the <code>event</code>. </p> </div> </div> </div> </div> <div id="footer"> <div class="foot-1"> <a href="http://www.joyent.com"><h5>The Node.js Project is Sponsored by</h5> <img src="assets/joyent-footer.svg" width="200"> <p class="tag">Production Node +<br>High Performance Infrastructure</p></a> <a href="https://my.joyent.com/landing/signup/701800000015696" class="button getstarted">Get Started</a> </div> <div class="foot-2"> <div class="foot-nav"> <ul> <li><a href="http://nodejs.org/download/">Downloads</a></li> </ul> <ul> <li><a href="http://nodejs.org/documentation/">Documentation</a></li> <li><a href="http://nodejs.org/api/">API Docs</a></li> <li><a href="http://nodejs.org/documentation/tutorials/">Tutorials</a></li> <li><a href="http://nodejs.org/documentation/localization/">Localization</a></li> </ul> <ul> <li><a href="http://nodejs.org/community/">Community</a></li> <li><a href="https://github.com/joyent/node/issues">Github Issues</a></li> <li><a href="http://groups.google.com/group/nodejs">Mailing List</a></li> <li><a href="http://webchat.freenode.net/?channels=node.js">IRC</a></li> <li><a href="https://twitter.com/nodejs">Twitter</a></li> </ul> <ul> <li><a href="http://nodejs.org/about/">About</a></li> <li><a href="http://nodejs.org/about/organization/">Organization</a></li> <li><a href="http://nodejs.org/about/core-team/">Core Team</a></li> <li><a href="http://nodejs.org/about/resources/">Resources</a></li> </ul> <ul> <li><a href="http://blog.nodejs.org">Blog</a></li> </ul> </div> <p class="copyright">Copyright 2014 <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="https://nodejs.org/images/trademark-policy.pdf">trademark</a> of Joyent, Inc. <a href="https://raw.github.com/joyent/node/v0.10.35/LICENSE">View license</a>.</p> </div> </div> <script src="/sh_main.js"></script> <script src="/sh_javascript.min.js"></script> <script>highlight(undefined, undefined, 'pre');</script> <script> window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']]; (function(d, t) { var g = d.createElement(t), s = d.getElementsByTagName(t)[0]; g.src = '//www.google-analytics.com/ga.js'; s.parentNode.insertBefore(g, s); }(document, 'script')); </script> </body> </html>