Hello, World! A first look at the library Node.js® for Cloudant
Transcription
Hello, World! A first look at the library Node.js® for Cloudant
Hello, World! A first look at the library ® Node.js for Cloudant Mike Broberg Director of Marketing Communications, Cloudant, an IBM Company Jason Smith Developer Advocate, Cloudant, an IBM Company Agenda • About Node.js and Cloudant • Why they’re best friends • Resources • Intro to the library Node.js for Cloudant • Questions 2 Node.js and Cloudant • Node.js • Implements everything that’s not already built into JavaScript for writing server-side apps • Great at building fast, interactive Web apps • Cloudant • NoSQL JSON document data store, accessed via an HTTP API and delivered as a fully managed database service (DBaaS) • Scales big, both in terms of data and concurrent users 3 Node.js: Server-Side and Small • “Asynchronous I/O library for file, socket, and HTTP communication” • Thanks, Wikipedia • People use Node.js to deal with server processes, and import only the modules they need from Node Package Manager (npm) • One app generally depends on dozens of Node.js modules 4 Node.js: Asynchronous, Rethought • Moar I/O! • Most apps are I/O-bound • In Node.js, I/O is non-blocking • Instead of multiple processes/threads to perform work, Node.js uses an event loop • Node.js is single-threaded, but event-based • Can handle many I/O connections in a single thread • I/O is event-driven & asynchronous • Everything runs in parallel, except your code • More from blog.mixu.net and Tom Hughes-Croucher (thanks) 5 Node.js: Stream It Like a Boss User client Node.js server DB • Minimize client response time • Good for simple Web apps that stream data & asynchronicity is OK • News feeds or collaboration apps • Not cars or missile silos 6 Cloudant: JavaScript Object Notation • Cloudant is based on Apache CouchDB™ and represents records as JSON documents {! "docs": [! {! "_id": "df8cecd9809662d08eb853989a5ca2f2",! "_rev": "1-8522c9a1d9570566d96b7f7171623270",! "Movie_runtime": 162,! "Movie_rating": "PG-13",! "Person_name": "Zoe Saldana",! "Actor_actor_id": "0757855",! "Movie_genre": "AVYS",! "Movie_name": "Avatar",! "Actor_movie_id": "0499549",! "Movie_earnings_rank": "1",! "Person_pob": "New Jersey, USA",! "Person_id": "0757855",! "Movie_id": "0499549",! "Movie_year": 2009,! "Person_dob": "1978-06-19"! }! ]! }! 7 JavaScript All the Things • JavaScript on the client, Node.js on the server, and JSON in the database Single-Language Stack Cloudant or CouchDB Angular.js Node.js Express https://twitter.com/filmaj/statuses/415615280069353472 8 Messy Data • Interactive Web apps create messy data • The flexible, self-describing schema of JSON documents handles it well • Flexible schema allows for fast agile development to add new features and data to applications without the need for downtime or locking, like an RDBMS 9 Cloudant: (Pop and) No Locking • Cloudant does not enforce locking of records • Append-only data store • Don’t update documents in place; instead post new versions of the same doc, incrementing reserved "_rev": field • Cloudant designed to be highly available, durable DB • Stores many copies of data across different physical infrastructure • Makes all copies available for reads & writes • Quorum protocol resolves changes behind the scenes • Eventually consistent 10 Besties • Asynchronous code + DB that doesn’t lock records = best friends • Support lots of concurrent I/O connections, process lots of data streams, make lots of application users very happy 11 Recap • Node.js popular for interactive Web apps, which is a great fit for a document store • JSON • Lots of templates and code modules available • npm • Single-language stack in JavaScript • CANE • Asynchronous code with a database that doesn't lock • Non-blocking I/O operations in Node.js • Non-locking database in Cloudant/CouchDB 12 Resources – Useful Tools • Cloudant blog on getting started with Node.js: https://cloudant.com/blog/using-cloudant-with-node-js/ • Third-party libraries: • nano - https://github.com/dscape/nano • sag - https://github.com/sbisbee/sag-js • cradle - https://github.com/flatiron/cradle • Template for Express framework: https://github.com/cloudant-labs/express-cloudant • Tool to upload spreadsheet/csv into Cloudant: https://github.com/garbados/Cloudant-Uploader • Getting started with Node.js on Bluemix: http://thoughtsoncloud.com/2014/07/getting-started-ibm-bluemix-node-js/ 13 Resources – Development Examples • Node.js CRUD example with Cloudant on Haengematte: https://github.com/cloudant/haengematte/tree/master/nodejs • Twilio blog on building a Node.js app using Cloudant: https://www.twilio.com/blog/2012/09/building-a-real-time-smsvoting-app-part-1-node-js-couchdb.html • Bluemix blog on building a simple word game: http://www.ibm.com/developerworks/cloud/library/clguesstheword-app/index.html?ca=drs• Article on integrating Rasberry Pi/Node.js/Bluemix: http://www.ibm.com/developerworks/library/ba-remoteservpi-app/ index.html 14 Introducing Node.js for Cloudant Cloudant’s first officially supported language-specific library • GitHub: https://github.com/cloudant/nodejs-cloudant • npm: https://www.npmjs.org/package/cloudant • A live code example with Jason Smith (@_jhs) • voxel-deathmatch: https://github.com/jhs/voxel-deathmatch 15 Thank You mbroberg@us.ibm.com @mikebroberg @cloudant jason@cloudant.com @_jhs