node-addon-examples icon indicating copy to clipboard operation
node-addon-examples copied to clipboard

EventEmitter

Open gagle opened this issue 12 years ago • 9 comments

Hi,

Is it possible to add another example showing how to emit events from the c++ land?

Here's an example done by bnoordhuis 2 years ago. Maybe you could rewrite it for v0.10 and v0.11?

The basic idea is to expose a c++ class which inherits from ObjectWrap and internally calls to the emit function and in the js code inherit from EventEmitter.

gagle avatar Jul 23 '13 20:07 gagle

I'll look in to that, I would normally say that you should abstract those details into JS-land where it's much easier but that example from Ben looks interesting.

rvagg avatar Jul 23 '13 23:07 rvagg

The example from bnoordhuis has a recent open pull request with a more modern example:

https://github.com/bnoordhuis/node-event-emitter/pull/2

kevzettler avatar Aug 04 '13 21:08 kevzettler

Wasn't EventEmitter removed? joyent/node@4ef8f06fe62edb74fded0e817266cb6398e69f36 IIRC, you are simply not supposed to emit events from native-land. It should be done with javascript. The easiest way being to define a js callback for native-land that emits the event.

kkoopa avatar Aug 06 '13 13:08 kkoopa

EventEmitter is alive and well. That linked example effectively does what you're describing. Do the EE stuff in JS and have simple hooks into C++. Still a worthwhile example because EE is such a common pattern.

rvagg avatar Aug 06 '13 14:08 rvagg

Oh my. In that case, zmq does it quite nicely. https://github.com/JustinTulloss/zeromq.node/blob/96bd7811a41eaac8f30dfed58769752f55ab2e3d/binding.cc#L304-322

but porting the mentioned example should be very easy.

kkoopa avatar Aug 06 '13 14:08 kkoopa

+1 on an example with nan - this would significantly reduce my learning curve.

To my eyes, the NanAsyncWorker class is built for single callback execution, whereas an event emitter will trigger callbacks multiple times - I'm not seeing a clear way to do that with nan

davidmarkclements avatar May 30 '15 02:05 davidmarkclements

@davidmarkclements that's exactly what I was thinking, I'd like to run a long running thread behind the screen which can emit events. I checked on nan project @bnoordhuis suggested to use uv_thread_create for doing something long running instead of NanAsyncWorker. It would be great if nan can abstract that functionality.

opensourcegeek avatar Jun 29 '15 10:06 opensourcegeek

+1

hokein avatar Jul 19 '15 10:07 hokein

What you just need is to use NanAsyncProgressWorker. It's a NanAsyncQueueWorker but with progress callbacks through libuv's uv_async_send functionality. Whenever you call progress->Send(char *, size); it will be caught by your devirtualized function 'HandleProgressCallback()' that will be executed in your main event loop. This can then for example execute a callback that'll invoke something in your .js. What is asked by the author however, is how to pass these along so that we have on.<> events.

TimZaman avatar Jul 28 '15 13:07 TimZaman