Node.js Domain Will Not Catch Custom Event Emitted

87 views
Skip to first unread message

dman777

unread,
Jun 10, 2014, 6:27:58 PM6/10/14
to nod...@googlegroups.com
I am new to Node.js domains and I am experimenting with them. I am trying to get mainDomain to display a message on database that is successful. Right now, there are no errors and everything runs ok when I start express.js. But the message does not show as it should after a successful database connection. I  can successfully get it to catch event error if I use ee.emit('error') instead of ee.emit('message', 1, 2, 3);. I don't understand why I can't emit ee.emit('message') and have it catch it.

var express = require('express');
var morgan = require('morgan');
var fs = require("fs");
var createDomain = require("domain").create;
var app = express();

var db = require(__dirname + '/app/data/db.js');
var mainDomain = new createDomain();
mainDomain.run(function () { 
    db.connectDatabase(); 
    mainDomain.on('message', function(msg) {
        console.log('yoyoyoyo');
    });
});
mainDomain.run(function () {
    mainDomain.on('error', function(er) {
        console.log('error, but oh well', er.message);
    });
    var server = app.listen(9000, function() {
            console.log('Listening on port %d', server.address().port);
    });
});


-----------------------------------------------------------------------------------------------------------------------

db.js:


var mongoose = require( 'mongoose' );
var EventEmitter = require('events').EventEmitter;
var ee = new EventEmitter();

function connectDatabase() {
    var dbURI = 'mongodb://localhost/database';
    mongoose.connect(dbURI);
    mongoose.connection.on('connected', function () {
        ee.emit('message', 1, 2, 3);
       // console.log('Mongoose default connection open to ' + dbURI);
    });
    mongoose.connection.on('error',function (err) {
        console.log('Mongoose default connection error: ' + err);
    });
    mongoose.connection.on('disconnected', function () {
        console.log('Mongoose default connection disconnected');
    });
    process.on('SIGINT', function() {
        mongoose.connection.close(function () {
            console.log('Mongoose default connection disconnected through' +
                        ' app termination');
            process.exit(0);
        });
    });
}   
module.exports.connectDatabase = connectDatabase;

Andrey

unread,
Jun 11, 2014, 2:28:08 AM6/11/14
to nod...@googlegroups.com
Domains only emit "error" event and are not designed for intercepting all event emitters messages but a way to route exception handling code. Try to replace "ee.emit('message', 1, 2, 3);" with "throw new Error('blah')" and you'll notice it's magically appearing in app.js mainDomain 'error' handler.
The reason this is possible is because each time async function callback is about to be executed process.domain is set to corresponding domain and try/catch around it calls 'error' handler.
This happens automatically for all core async ( timers, net, files ) but you might need to add domains support manually in some other cases ( like binary modules + libuv async ) 
Reply all
Reply to author
Forward
0 new messages