Appcelerator Node.ACS
Adding server-side functionality to share data between apps
within the Appcelerator ecosystem
TiLond...
WHAT is node.js?
“Node.js is a platform built on Chrome's JavaScript runtime for easily
building fast, scalable network ap...
WHy would you use it?
APIs
Web service
endpoints
Service 1
Service 2
Service 3
We want to
talk to
external
services to
ret...
Why would you use it?
- orchestration
APIs
Web service
endpoints
Reduce messages - each can take 0.5s to create and tear
d...
WHy would you use it?
- Abstraction
APIs
Web service
endpoints
Swap out 3rd party suppliers without redeploying the app
Cr...
WHy would you use it?
- integration & minification
APIs
Web service
endpoints
Simplify complex API responses into small JS...
creating a node.js webservice
To create a new widget, right click the project name in the Titanium Studio
project view... ...
Project file structure
Doing it this way puts ACS in the same project tree; particularly useful when
using version control...
TESTING The service
Right click the project name in the Titanium Studio project view... and select
Publish Publish Node.AC...
TESTING The service
If you are sending sensitive data, change from http:// to https://. We often
just publish directly to ...
Project file structure
The “controllers” folder
holds a file “application.js” by
default; we use this to hold
our web serv...
TESTING The service
Use the public url you stored in TiApp.xml and post it into a browser; you
should see the services run...
Building services
To create a new service, right click the project name in the Titanium Studio
project view... and select ...
Building services
To create a new service, right click the project name in the Titanium Studio
project view... and select ...
Building services
You will see a new file “services.js” has been created and the “testGet”
method added.
“config.js” will ...
Building services
Manually create another method “testPost” and manually modify “config.js”
as well.
don’t forget to chang...
passing data
GET requests pass in data via.
the query string
POST requests pass in data via.
the request body
the “req” ob...
asynchronous methods
node.js is a great, lightweight server; but you need to understand how to
make the best use of it.
Wh...
configuring the client
Publish the new node.js node changes into production.
configuring the client
This auto-generates the bindings that the app needs to connect to the
node.js server. It puts the b...
configuring the client
If you add new web services you can either repeat and rinse this process, or
simply add the extra m...
configuring the client
In alloy.js you will see we have created the reference to our service.
Edit this to create a global...
configuring the client
If you look in the bindings file that was auto generated you will see our new
web service methods.
editing the bindings
Unfortunately, the auto-generated file won’t handle get requests that have
parameters in the query st...
editing the bindings
and modify the get method to push the serialized data into the query string.
Oh and remember to remov...
Testing the code
Create a test harness in index.xml and index.js as follows:
index.xml index.js
Testing the code
Thank you
Source code: http://bit.ly/1jZHJvb
Slideshare: http://slidesha.re/1myDc7s
check out our other slideshares:
Slide...
Upcoming SlideShare
Loading in …5
×

Appcelerator - using node.ACS (part 1)

1,935 views

Published on

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.

Published in: Mobile, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,935
On SlideShare
0
From Embeds
0
Number of Embeds
53
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Appcelerator - using node.ACS (part 1)

  1. 1. Appcelerator Node.ACS Adding server-side functionality to share data between apps within the Appcelerator ecosystem TiLondon Martin Hudson March 2014
  2. 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. 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. 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. 5. WHy would you use it? - Abstraction APIs Web service endpoints Swap out 3rd party suppliers without redeploying the app Credit card process
  6. 6. WHy would you use it? - integration & minification APIs Web service endpoints Simplify complex API responses into small JSON packets
  7. 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. 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. 9. TESTING The service Right click the project name in the Titanium Studio project view... and select Publish Publish Node.ACS Service.→
  10. 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. 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. 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. 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. 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. 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. 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. 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. 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.
  19. 19. configuring the client Publish the new node.js node changes into production.
  20. 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. 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. 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. 23. configuring the client If you look in the bindings file that was auto generated you will see our new web service methods.
  24. 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. 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. 26. Testing the code Create a test harness in index.xml and index.js as follows: index.xml index.js
  27. 27. Testing the code
  28. 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

×