Skip to content
This repository
Browse code

updated content after feedback from Tim

  • Loading branch information...
commit b8198dcf4be53d5f8a0b33e372789e6f04d86c99 1 parent 21c3252
____________. authored August 08, 2010
27  articles/demystifying-events-in-node.markdown
Source Rendered
... ...
@@ -1,10 +1,12 @@
1 1
 Title: Demystifying events in node.js
2  
-Author: Kenny
  2
+Author: Kenny Shen
3 3
 Date: Sun Aug 08 2010 01:48:53 GMT+0800
4 4
 Node: v0.1.102
5 5
 
6 6
 Ok, here's an important thing to understand when you're working with node.js. There're lots of node objects that emit events, and you can easily find some examples on the documentation. But what's not really clear perhaps is how do you write your own events and listeners. You could get by for a while without this, but then you'll soon reach a wall. So how do you write them? The important friend you need to know first of all is the 'events' module in node.js.
7 7
 
  8
+## The quick overview
  9
+
8 10
 To access this module, you simply add this to your js file:
9 11
 
10 12
 > require('events')
@@ -13,25 +15,34 @@ To access this module, you simply add this to your js file:
13 15
 
14 16
 Specially, the docs will tell you the later is that all objects that emit events are basically instances of the latter. Let's create a simple dummy,
15 17
 
16  
-<demystifying-events-in-node/dummy.js*>
  18
+<demystifying-events-in-node/dummy.js>
17 19
 
18 20
 The important part here is that we're extending our object with EventEmitter, and thus inheriting all the prototype objects,methods...etc from it.
19 21
 
20  
-So now let's assumy Dummy needs to have a method call cooking(), and once he puts the chicken to roast, he wants an event to be emitted, e.g. 'cooked', and fire a callback call 'eat'. (yum, roast chix! :P)
  22
+So now let's assume Dummy needs to have a method call cooking(), and once he puts the chicken to roast, he wants an event to be emitted, e.g. 'cooked', and fire a callback call 'eat'. (yum, roast chix! :P)
21 23
 
22  
-<demystifying-events-in-node/dummy-cooking.js*>
  24
+<demystifying-events-in-node/dummy-cooking.js>
23 25
 
24 26
 So we're done with this module. Let's say we want to use it in our main script,
25 27
 
26  
-<demystifying-events-in-node/dummy-node.js*>
  28
+<demystifying-events-in-node/dummy-node.js>
27 29
 
28 30
 So basically, node.js runs through the couple of lines and then waits for the event 'cooked' to be emitted, upon which it then fires off the callback while passing it the returned argument(s).
29 31
 
  32
+## Where to go from here
  33
+
  34
+It's worthy to note that "sub-classing" in the examples given here are a bit of an overkill, as well as using events.EventEmitter for things that only fire one event once. It's probably better to put the new methods on the instance if there's only going to be a few of them and for firing a low level of events, you might want to use asynchronous functions instead.
  35
+
  36
+Regarding events.EventEmitter, there's a special event that you want to note - 'error'. This is fired whenever an error is encountered, and is special in the sense that if no listeners are attached it, node will throw an exception and terminate the execution. (thanks to Tim for highlighting this.)
  37
+
  38
+## Before I go eat my chix...
  39
+
30 40
 The docs has some good runthrough, and I helped myself by searching through my favorite libraries and looking at how others implemented it, and also received some good answers that helped clear this up after posting to the mailing list. I suggest taking a read of Tim Caswell's articles for a better insight to all this node-ty stuff:
31 41
 
32  
-    * [Control Flow in Node Part 2](/control-flow-part-ii)
33  
-    * [What is "this"?](/what-is-this)
  42
+[Control Flow in Node Part 2](/control-flow-part-ii)
  43
+
  44
+[What is "this"?](/what-is-this)
34 45
 
35 46
 In the meanwhile, you can also view my crazy goofy sketch of a ircbot + logger + real-time websockets + search enabled bot mashup in node.js - [tocho](http://github.com/kennyshen/tocho)! I had a lot of fun making him, and while I haven't posted the search feature to the demo site, you can lurk through the #node.js irc channel with a web sockets enabled browser in real-time [here](http://northpole.sg/3Z).
36 47
 
37  
-Hope you found this article useful.
  48
+Hope you found this article useful, and do comment below if you have any suggestions on adding to this (I'm sure I might have missed out something)
4  articles/demystifying-events-in-node/dummy-node.js
... ...
@@ -1,7 +1,7 @@
1 1
 // A nonsensical node.js program
2 2
 
3  
-var dummy = require('./dummy');
4  
-var kenny = new dummy.Dummy();
  3
+var Dummy = require('./dummy');
  4
+var kenny = new Dummy();
5 5
 var dinner = kenny.cooking(fried_chix);
6 6
 dinner.on('cooked', function(chicken) {
7 7
     // eat up!
15  articles/demystifying-events-in-node/dummy.js
... ...
@@ -1,13 +1,18 @@
1 1
 // basic imports
2  
-var sys = require('sys');
3 2
 var events = require('events');
4 3
 
5 4
 // for us to do a require later
6  
-exports.Dummy = Dummy;
  5
+module.exports = Dummy;
7 6
 
8 7
 function Dummy() {
9  
-    var self = this;
10  
-    events.EventEmitter.call(self);
  8
+    events.EventEmitter.call(this);
11 9
 }
12 10
 
13  
-sys.inherits(Dummy, events.EventEmitter);
  11
+// inherit events.EventEmitter
  12
+Dummy.super_ = events.EventEmitter;
  13
+Dummy.prototype = Object.create(events.EventEmitter.prototype, {
  14
+    constructor: {
  15
+        value: Dummy,
  16
+        enumerable: false
  17
+    }
  18
+});

0 notes on commit b8198dc

Please sign in to comment.
Something went wrong with that request. Please try again.