Reputation: 4250
Does Meteor.js have a way to emit global events on the server ? Eg
// how to set make myEmitter available across files/packages ?
myEmitter = new Npm.require('events').EventEmitter;
if(Meteor.isServer){
Meteor.methods({
'foo' : function(){
myEmitter.emit('fooEvent', { 'bar' : 'hello!'});
}
});
}
and
if(Meteor.isServer){
function doFoo(){
console.log('Foo done !');
}
myEmitter.on('fooEvent', doFoo);
}
Upvotes: 1
Views: 621
Reputation: 75945
Your code should work. Just change this line:
myEmitter = new Npm.require('events').EventEmitter;
to
myEmitter = new (Npm.require('events').EventEmitter);
You shouldn't have to use a package this because EventEmitter is part of nodejs core.
To make it available to the other files just don't use the var
keyword.
If you're using this in a package you have to make sure you use api.export
in your package.js
to export it out to the rest of the app.
Upvotes: 3
Reputation: 2978
Meteor doesn't have a built-in event emitter. However, it does allow you to use npm modules, like the one in your example code.
You'll need to add the npm package to your app. See it's documentation on atmosphere for details.
Upvotes: 2