C++ listener addon example?

328 views
Skip to first unread message

kevzettler

unread,
Jul 30, 2013, 7:22:30 PM7/30/13
to nod...@googlegroups.com
Hi i'm writing a node wrapper for a C level library. I'd like to structure the the Node API as a standard node event emitter something like c_lib.on("event", callback) Wondering if anyone has experience doing this and maybe an example I can eyeball. So far all i've found as far as examples are one time async callback addons like c_lib.method(callback(){}).

Floby

unread,
Jul 31, 2013, 8:42:55 AM7/31/13
to nod...@googlegroups.com
In recent versions of Node.js, the native EventEmitter class is pure Javascript. So you'll have to instanciate it from the JS constructor.

Anyway, if the library you're writing bindings to is C, you should just do the smallest work in C and do the rest in JS.
see this example from node-libspotify
https://github.com/Floby/node-libspotify/blob/master/lib/Player.js

On Wednesday, 31 July 2013 01:22:30 UTC+2, kevzettler wrote:
Hi i'm writing a node wrapper for a C level library. I'd like to structure the the Node API as a standard node event emitter something like c_lib.on("event", callback) Wondering if anyone has experience doing this and maybe an example I can eyeball. So far all i've found as far as examples are one time async callback addons like c_lib.method(callbache k(){}).

Gagle

unread,
Aug 3, 2013, 1:55:48 PM8/3/13
to nod...@googlegroups.com

Stephen Vickers

unread,
Aug 5, 2013, 5:54:14 PM8/5/13
to nod...@googlegroups.com
The raw-socket module does something like this:


It sub-classes from EventEmitter and emits events such as "close" and "error" from the c++ code, e.g. the close event:


The c++ module itself needs to inherit from EventEmitter, I found this easiest to do from within JavaScript:


But to make it work fully you need to do this (at least I had to do this at the time):


Steve

Reply all
Reply to author
Forward
0 new messages