Created
August 17, 2011 15:44
-
-
Save mheadd/1151820 to your computer and use it in GitHub Desktop.
Node Knockout Demo Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Enter your configuration details in this file. | |
| */ | |
| // Details of your CouchDB instance. | |
| exports.couchdb = { | |
| userid : '', | |
| password : '', | |
| host : 'http://127.0.0.1', | |
| port : '5984', | |
| dbname : '' | |
| }; | |
| // Your SMSified credentials - free signup at https://smsified.com/signup | |
| exports.smsified = { | |
| username : '', | |
| password : '', | |
| senderAddress : '' | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Node.js class that watches the CouchDB _changes API and emits events on new changes. | |
| */ | |
| var EventEmitter = require('events').EventEmitter; | |
| var cradle = require('cradle'); | |
| CouchWatcher = function(options) { | |
| this.userid = options.userid; | |
| this.password = options.password; | |
| this.dbname = options.dbname; | |
| this.host = options.host || '127.0.0.1'; | |
| this.port = options.port || 5984; | |
| this.heartbeat = options.heartbeat || 1000; | |
| }; | |
| CouchWatcher.prototype = new EventEmitter; | |
| CouchWatcher.prototype.watch = function(filter, since) { | |
| var self = this; | |
| // Create new connection to CouchDB instance. | |
| var db = new (cradle.Connection)(self.host, self.port, | |
| { | |
| auth : { | |
| username : self.userid, | |
| password : self.password | |
| } | |
| }).database(self.dbname); | |
| // Watch changes API and emit event with document. | |
| var options = { include_docs : true, feed : 'continuous', heartbeat : self.heartbeat}; | |
| if(filter) { | |
| options.filter = filter; | |
| } | |
| if(since) { | |
| options.since = since; | |
| } | |
| db.changes(options).on('response', function(res) { | |
| res.on('data', function(change) { | |
| self.emit('doc', change.doc); | |
| }); | |
| res.on('end', function() { | |
| self.emit('end'); | |
| }); | |
| }); | |
| }; | |
| exports.CouchWatcher = CouchWatcher; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * JSON for a CouchDB filter called "newdoc" | |
| * more on CouchDB _changes API and filters at http://guide.couchdb.org/editions/1/en/notifications.html#filters | |
| */ | |
| { | |
| "newdoc": "function(doc, req) { if(doc._rev.charAt(0) == '1') { return true; } return false;}" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A simple SMSified app built with Node.js and CouchDB. | |
| */ | |
| var http = require('http'); | |
| var sys = require('sys'); | |
| var config = require('./config'); | |
| var CouchWatcher = require('./couch-watcher').CouchWatcher; | |
| var smsified = require('smsified'); | |
| var cradle = require('cradle'); | |
| // Function to save / update a doc in CouchDB. | |
| function saveDoc(doc) { | |
| db.save(doc, function(err, res) { | |
| if (err) { | |
| sys.puts('Could not save document.'); | |
| } else { | |
| sys.puts('Document saved: ' + res.id); | |
| } | |
| }); | |
| } | |
| // CouchDB changes watcher. | |
| var responder = new CouchWatcher(config.couchdb); | |
| responder.watch('app/newdoc'); | |
| // Event handler for doc delivered from CouchDB changes API. | |
| responder.addListener('doc', function(doc) { | |
| var sms = new SMSified(config.smsified.username, config.smsified.password); | |
| // The reply to be sent to incoming SMS messages. This can be whatever you want, based on app logic or business rules. | |
| var reply = 'Node.js rocks!!'; | |
| var options = {senderAddress: config.smsified.senderAddress, address: doc.senderAddress, message: reply}; | |
| sms.sendMessage(options, function(result) { | |
| doc.reply = reply; | |
| doc.sendResult = result; | |
| saveDoc(doc); | |
| }); | |
| }); | |
| // Port the web server will run on. | |
| var port = process.argv[2] || 8000; | |
| // Connection to CouchDB for saving docs. | |
| var db = new (cradle.Connection)(config.couchdb.host, config.couchdb.port, { | |
| auth : { | |
| username : config.couchdb.userid, | |
| password : config.couchdb.password | |
| } | |
| }).database(config.couchdb.dbname); | |
| // Web server to listen for incoming HTTP POSTs from SMSified. | |
| var server = http.createServer(function(req, res) { | |
| req.addListener('data', function(data) { | |
| var inbound = new InboundMessage(JSON.parse(data)); | |
| saveDoc(inbound); | |
| }); | |
| res.writeHead(200); | |
| res.end(); | |
| }).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment