Node.js, how to avoid event namespace conflict?

Tag: node.js Author: connieli8687 Date: 2010-09-02

In Android, intent action string is always prefixed with package name to avoid conflict. what is the pattern in node.js ?

Other Answer1

Ok, I guess event is not broadcasted, it is a callback on certain object.

Other Answer2

Event is not broadcasting but a msg delivered to destinated component.

argument_object = { method:'post', onComplete: function() { self.end() } }  
dest_component.emit("event", argument_object);  
dest_component.on('event', function(argument_object){
    // 
}

Event Architecture layer

First, everything starts from a web sock

var sock = io.listen()  

sock.on("connection", function(client) {  
    client.on("message", function(msg) { (msg='join') new Player(client) }  
    client.on("disconnect", function() { }  
}  

Each web sock client wrapped into a player, handle sock client msg with upper layer player's emit event.

function Player(client) {  
    client.on("message", this.handleMsg.bind(this);  
}  

Player.prototype.handleMsg = function(event) {  
    this.emit('event', event, this);  
}

Game contains a set of players. Player's event handler using upper layer Game's function.

Game = module.exports.Game = function() { }  

Game.prototype.add = function(player) {  
   player.on("event", this.notify.bind(this);  
}  

Game.prototype.notify = function(msg, eventsrc) {  
   for(var player in players)  
       player.send(encode(msg));  
   }
}