28
Appcelerator Node.ACS Adding server-side functionality to share data between apps within the Appcelerator ecosystem TiLondon Martin Hudson March 2014

Appcelerator - using node.ACS (part 1)

Embed Size (px)

DESCRIPTION

A node.js server is baked into Titanium Studio. What is it and why should you use it? This guide explains how to set up and use node.ACS in your Alloy projects.

Citation preview

Page 1: Appcelerator - using node.ACS (part 1)

Appcelerator Node.ACSAdding server-side functionality to share data

between apps within the Appcelerator ecosystem

TiLondonMartin Hudson

March 2014

Page 2: Appcelerator - using node.ACS (part 1)

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)

Page 3: Appcelerator - using node.ACS (part 1)

WHy would you use it?

APIs

Web service

endpointsService 1

Service 2

Service 3

We want to talk to

external services to

retrieve information

Page 4: Appcelerator - using node.ACS (part 1)

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!

Page 5: Appcelerator - using node.ACS (part 1)

WHy would you use it?- Abstraction

APIs

Web service

endpoints

Swap out 3rd party suppliers without redeploying the app

Credit card

process

Page 6: Appcelerator - using node.ACS (part 1)

WHy would you use it?- integration & minification

APIs

Web service

endpoints

Simplify complex API responses into small JSON packets

Page 7: Appcelerator - using node.ACS (part 1)

creating a node.js webserviceTo 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.

Page 8: Appcelerator - using node.ACS (part 1)

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

Page 9: Appcelerator - using node.ACS (part 1)

TESTING The serviceRight click the project name in the Titanium Studio project view... and select Publish → Publish Node.ACS Service.

Page 10: Appcelerator - using node.ACS (part 1)

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.

Page 11: Appcelerator - using node.ACS (part 1)

Project file structureThe “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.

Page 12: Appcelerator - using node.ACS (part 1)

TESTING The serviceUse 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!' });}

Page 13: Appcelerator - using node.ACS (part 1)

Building servicesTo create a new service, right click the project name in the Titanium Studio project view... and select New → Node.ACS Method.

Page 14: Appcelerator - using node.ACS (part 1)

Building servicesTo 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”

Page 15: Appcelerator - using node.ACS (part 1)

Building servicesYou 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.

Page 16: Appcelerator - using node.ACS (part 1)

Building servicesManually 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”

Page 17: Appcelerator - using node.ACS (part 1)

passing dataGET 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.

Page 18: Appcelerator - using node.ACS (part 1)

asynchronous methodsnode.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.

Page 19: Appcelerator - using node.ACS (part 1)

configuring the clientPublish the new node.js node changes into production.

Page 20: Appcelerator - using node.ACS (part 1)

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

Page 21: Appcelerator - using node.ACS (part 1)

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.

Page 22: Appcelerator - using node.ACS (part 1)

configuring the clientIn 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.

Page 23: Appcelerator - using node.ACS (part 1)

configuring the clientIf you look in the bindings file that was auto generated you will see our new web service methods.

Page 24: Appcelerator - using node.ACS (part 1)

editing the bindingsUnfortunately, 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...

Page 25: Appcelerator - using node.ACS (part 1)

editing the bindingsand 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...

Page 26: Appcelerator - using node.ACS (part 1)

Testing the codeCreate a test harness in index.xml and index.js as follows:

index.xml index.js

Page 27: Appcelerator - using node.ACS (part 1)

Testing the code

Page 28: Appcelerator - using node.ACS (part 1)

Thank youSource 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 consultancywww.mobiledatasystems.comartin.hudson@mobiledatasystems.co