43
Node, native code and Couchnode

node.js and native code extensions by example

Embed Size (px)

DESCRIPTION

Over the last years node.js has evolved to be a great language to build web applications. The reason for this is not only that it is based on JavaScript which already is established around "the web" but also that it provides excellent facilities for extensions, not only via JavaScript but also integration of native C libraries. Couchbase makes a lot of use of this fact making the Couchbase node.js SDK (Couchnode) a wrapper around the C library providing a node.js like API, but leveraging the power of a native C library underneat. So how is this done? How does such a package look like? Let me show you how integration of C in node.js works and how to "read" a package like Couchnode.

Citation preview

Page 1: node.js and native code extensions by example

Node, native code and Couchnode

Page 2: node.js and native code extensions by example

PHILIPP FEHRE

Twitter: @ischi

Github: @sideshowcoder

Developer Advocate, Couchbase

Page 3: node.js and native code extensions by example

NODE.JS AND NATIVE CODE

Using C/C++ from node

Page 4: node.js and native code extensions by example

Why would I want to do this?

Page 5: node.js and native code extensions by example

Of course Speed!

Page 6: node.js and native code extensions by example

Nope…

Page 7: node.js and native code extensions by example

C is cool!

Page 8: node.js and native code extensions by example
Page 9: node.js and native code extensions by example

USING A C LIBRARY FROM NODE.JS

Probably the best reason

Page 10: node.js and native code extensions by example

Couchnode is one of those cases

Based on libcouchbase + wrapper

Page 11: node.js and native code extensions by example

Quick: What’s Couchbase?

Page 12: node.js and native code extensions by example

A Document Database.

Page 13: node.js and native code extensions by example
Page 14: node.js and native code extensions by example

Focus on Performance

Allow flexibility by running JS

Page 15: node.js and native code extensions by example
Page 16: node.js and native code extensions by example

Using it from node.js

Page 17: node.js and native code extensions by example
Page 18: node.js and native code extensions by example
Page 19: node.js and native code extensions by example

What happens here?

Page 20: node.js and native code extensions by example
Page 21: node.js and native code extensions by example
Page 22: node.js and native code extensions by example
Page 23: node.js and native code extensions by example

Seriously I like to show code

But this starts to be a little much

Page 24: node.js and native code extensions by example

The hello world of extension

Page 25: node.js and native code extensions by example

0.10 vs 0.11

Everything changes, welcome to the world of nan

Page 26: node.js and native code extensions by example
Page 27: node.js and native code extensions by example
Page 28: node.js and native code extensions by example
Page 29: node.js and native code extensions by example

What could go wrong?

Lets explore it by wrapping a C lib for murmur3 hashing

Page 30: node.js and native code extensions by example

Wrapping C

Page 31: node.js and native code extensions by example
Page 32: node.js and native code extensions by example

Arguments and callbacks

Page 33: node.js and native code extensions by example
Page 34: node.js and native code extensions by example
Page 35: node.js and native code extensions by example

Some more couchnode

To finish of…

Page 36: node.js and native code extensions by example

What happens on a GET

Page 37: node.js and native code extensions by example
Page 38: node.js and native code extensions by example
Page 39: node.js and native code extensions by example

Why is cas an Array?

Page 40: node.js and native code extensions by example

Couchbase Server 3.0 Beta Bug Bash Download, Find, Report, Win

• DOWNLOAD Couchbase Server 3.0 Beta

• Run some workload, play with it, FIND a bug

• If you find a bug, REPORT it!

- All bugs count – anything from correctness, to performance, to usability, to docs

• WIN Prizes!!

http://www.couchbase.com/bugbash

Contest ends Aug 31st 2014

Page 41: node.js and native code extensions by example

Come to spanconf.io

Page 42: node.js and native code extensions by example

Thank you!

Any questions?

Page 43: node.js and native code extensions by example

Links

• https://github.com/kkaefer/node-cpp-modules

• https://github.com/TooTallNate/node-gyp

• https://github.com/rvagg/nan

• https://github.com/couchbase/couchnode

• http://www.couchbase.com/communities/nodejs

• https://github.com/sideshowcoder/nurmur/