Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit event in mongoose middleware?

I want to emit event when new blog saved

blog.post('save',function(blog){
    this.emit('newBlog',blog)
})

and somewhere else in my project say app.js can listen this event

EventEmitter  = require('events').EventEmitter;
emitter = new EventEmitter();

emitter.on('newBlog',function(blog){
    console.log(blog);
})

how to do this?

like image 342
paynestrike Avatar asked Jun 01 '15 09:06

paynestrike


People also ask

What is emit event?

An event emitter is a pattern that listens to a named event, fires a callback, then emits that event with a value. Sometimes this is referred to as a “pub/sub” model, or listener.

What does emit do in typescript?

Passing arguments and this to listeners# The eventEmitter. emit() method allows an arbitrary set of arguments to be passed to the listener functions.

What is middleware in Mongoose?

Mongoose middleware are functions that can intercept the process of the init , validate , save , and remove instance methods. Middleware are executed at the instance level and have two types: pre middleware and post middleware.

What is emit function in Javascript?

These objects expose an eventEmitter. on() function that allows one or more functions to be attached to named events emitted by the object. When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.


2 Answers

If you emit your event from an document (an instance of a specific Schema), then you must listen on this instance to receive the event.

If you want to listen globally on all events generated by all instances then you must emit the event from the Schema.

blog.post('save',function(blog){
    blog.schema.emit('newBlog',blog)
})

And in your app.js for example, you can listen for events on the schema:

Blog.schema.on('newBlog',function(blog){
    console.log(blog);
})
like image 199
Julien Bachmann Avatar answered Nov 15 '22 21:11

Julien Bachmann


The way event emitter works is that you have to use the same event emitter object to listen on that you used to emit. So you need something like this:

To share this among different parts of your project you should create a module out of it and require it wherever needed.

my-event.js:

var eventEmitter = new require('events').EventEmitter();
module.exports = eventEmitter; 

Then you require this eventEmitter wherever you want to use it

blog.js:

var myEvent = require('../my-event');
blog.post('save',function(blog){
    myEvent.emit('newBlog', blog);
});

app.js:

var myEvent = require('./my-event');
myEvent.on('newBlog', console.log);

If you don't want to go through the trouble of creating and requiring your own module, you can use the global process which is also an EventEmitter.

anywhere:

process.on('my-custom-event', myCustomHandler);

anywhere-else:

process.emit('my-custom-event', data);

Although a fair warning: you're polluting the global scope. If some other module is also doing the same and using the same event names then you have a conflict which may surface in unexpected manner and be even harder to debug.

like image 22
laggingreflex Avatar answered Nov 15 '22 23:11

laggingreflex