4 Flares 4 Flares ×
A few weeks ago we received a series of questions from the community about using Node.js with Titanium Desktop. Using a remote Node.js server with the Titanium mobile and/or desktop applications is not much different that working with any remote service. Working with a local Node.js server however is somewhat problematic in that most desktop machines do not have a local Node instance. That being said, while Titanium Desktop (as it stands today) does not bundle Node.js that doesn’t mean you can’t. Or at least that was my theory before kicking off a prototype a few days ago. screenshot To make Node.js useful from a local desktop development perspective we need to assure our app always has access to a Node.js instance. To accomplish this I started by compiling Node for OSX and Linux and dropping the resulting binaries into my projects default Resources directory. Items within the Resources directory are bundled with our project when we package for distribution so that solves the first problem. OK so we can distribute Node with the app now, but that is not of much use to us unless we have the ability within our application to pass it a script, create a server, etc. One of the great things about Titanium Desktop is the ability to use and/or combine the usage of JavaScript, PHP, Python, and Ruby. For this project I settled on utilizing Python & JavaScript. I wrote a basic Python script that is included when the application loads. This script detects the running platform, tracks down the right Node binary, spins up a Node child process for us and passes it a script to get things started. When the instance is ready a callback is made to JavaScript land and the real fun can begin. In this prototype the custom script I have Node execute creates a server and starts listening for incoming connections. Interacting with that instance can be done via standard Ajax using whatever JavaScript library you’re most comfortable with. To demonstrate usage of this I’ve built a simple little Github application. With Node support comes access to the wonderful world of Node Modules. Use can use npm to install modules within a project’s Resources directory just as you would any other Node project. This prototype uses the node-github module to handle interation with the Github API within Node. We’ve open sourced the project so head on over to Github and grab the source. If you just want to run the packaged versions you can grab those builds here. Fork, experiment and enjoy!