This document discusses Appcelerator's Node.ACS service, which allows adding server-side functionality to share data between apps. It provides an overview of Node.js and how Appcelerator integrates it. It then demonstrates how to create a Node.ACS service project, build APIs, test the service, configure the client, and call the service from an app. Key steps include generating a service, adding methods, publishing, importing bindings, and making requests.
2. WHAT is node.js?
“Node.js is a platform built on Chrome's JavaScript runtime for easily
building fast, scalable network applications. Node.js uses an event-driven,
non-blocking I/O model that makes it lightweight and efficient, perfect for
data-intensive real-time applications that run across distributed devices.”
nodejs.org
•Appcelerator have provided an out-of-the-box service embedded directly
into Titanium Studio.
•Fully integrates with Appcelerator Cloud Services (ACS)
3. WHy would you use it?
APIs
Web service
endpoints
Service 1
Service 2
Service 3
We want to
talk to
external
services to
retrieve
information
4. Why would you use it?
- orchestration
APIs
Web service
endpoints
Reduce messages - each can take 0.5s to create and tear
down over 3G network - then send the payload!
5. WHy would you use it?
- Abstraction
APIs
Web service
endpoints
Swap out 3rd party suppliers without redeploying the app
Credit
card
process
6. WHy would you use it?
- integration & minification
APIs
Web service
endpoints
Simplify complex API responses into small JSON packets
7. creating a node.js webservice
To create a new widget, right click the project name in the Titanium Studio
project view... and select New Node.ACS Service.→
Doing it this way puts ACS in the same project tree; particularly useful when
using version control as your app code is synched with the webservice.
8. Project file structure
Doing it this way puts ACS in the same project tree; particularly useful when
using version control as your app code is synched with the webservice.
An ACS folder is created; we are interested in the controllers and config.js
9. TESTING The service
Right click the project name in the Titanium Studio project view... and select
Publish Publish Node.ACS Service.→
10. TESTING The service
If you are sending sensitive data, change from http:// to https://. We often
just publish directly to the cloud and put debugging and error info into the
response stream. That way we can switch debugging on in production if
something mis-behaves.
In TiApp.xml you can change the location of the service from localhost to the
public url of the published service. This enables you to easily test on real
devices. Stay on localhost for debugging.
11. Project file structure
The “controllers” folder
holds a file “application.js” by
default; we use this to hold
our web services.
We can create as many
controllers as we like, each
containing logical groups of
web services.
“config.js” is used to map our web service methods to real-world urls.
We also specify if the service call uses PUSH or GET.
12. TESTING The service
Use the public url you stored in TiApp.xml and post it into a browser; you
should see the services running.
the “config.js” file shows the routing for the default call to the service and
points to { "path": "/", "callback": "application#index" }
in the controller, “application.js” contains the method “index”
function index(req, res) {
res.render('index', { title: 'Welcome to Node.ACS!' });
}
13. Building services
To create a new service, right click the project name in the Titanium Studio
project view... and select New Node.ACS Method.→
14. Building services
To create a new service, right click the project name in the Titanium Studio
project view... and select New Node.ACS Method.→
Specify the web method name
you want to expose, I’m going to
create a method called “testGet”
15. Building services
You will see a new file “services.js” has been created and the “testGet”
method added.
“config.js” will also have changed,
it now contains the routing to
new “testGet” method. By
default, the method is always set
to GET.
16. Building services
Manually create another method “testPost” and manually modify “config.js”
as well.
don’t forget to change the
“testPost” method from “get” to
“post” in “config.js”
17. passing data
GET requests pass in data via.
the query string
POST requests pass in data via.
the request body
the “req” object contains the http request, we construct the response and
put it into the “send” function of the “res” object. NOTE: we can simply
construct the JSON response and return it.
18. asynchronous methods
node.js is a great, lightweight server; but you need to understand how to
make the best use of it.
When we make a call and it is serviced by the server, the server blocks all
other incoming requests until it processes the current call.
If the service is to be scalable we need to ensure that requests process
quickly. For anything but the most trivial calls like the test examples we
created we need to process methods asynchronously.
20. configuring the client
This auto-generates the bindings that the app needs to connect to the
node.js server. It puts the bindings file into the “Resources” folder. If you
are developing in Alloy you will need to move it because it will be
overwritten by the Alloy compiler when you next do a clean build...
In the Titanium Studio project view select Node.ACS Import Node.ACS→
Bindings.
and select the service we have built.
Check the alloy.js file from the list and
click OK
21. configuring the client
If you add new web services you can either repeat and rinse this process, or
simply add the extra method references into bindings file.
We create a “lib” folder off the root of the “app” folder and put the bindings
file into it.
22. configuring the client
In alloy.js you will see we have created the reference to our service.
Edit this to create a global we can now reference from wherever we need
to.
23. configuring the client
If you look in the bindings file that was auto generated you will see our new
web service methods.
24. editing the bindings
Unfortunately, the auto-generated file won’t handle get requests that have
parameters in the query string. Add the following local function to the
bindings file...
25. editing the bindings
and modify the get method to push the serialized data into the query string.
Oh and remember to remove the “data” object from within “InvokeService”
too...
26. Testing the code
Create a test harness in index.xml and index.js as follows:
index.xml index.js
28. Thank you
Source code: http://bit.ly/1jZHJvb
Slideshare: http://slidesha.re/1myDc7s
check out our other slideshares:
Slideshare: http://bit.ly/alloy-customTableViewSlides
Mobile Data Systems Ltd.Turnkey mobile consultancy
www.mobiledatasystems.co
martin.hudson@mobiledatasystems.co