第1页
Node.js
Server Connect To API / 3rd Service Pattern
Monday, June 23, 14
Caesar Chi @clonncd
第2页
Monday, June 23, 14
@clonncd
第3页
Monday, June 23, 14
第4页
https://github.com/nodejs-tw/nodejs-wiki-book
Monday, June 23, 14
第5页
Monday, June 23, 14
第6页
Structure Clients
Server
Monday, June 23, 14
第7页
That means we thought done with something in Node.js But actually we did nothing special.
Monday, June 23, 14
第8页
Monday, June 23, 14
第9页
Request of
As a Front end Server
Monday, June 23, 14
第10页
Structure front Server
send Request
Services
secret
Monday, June 23, 14
information
cache
第11页
When Node.js has Request
WWW are APIs
Monday, June 23, 14
第12页
Structure front Server
send Request
Services
Monday, June 23, 14
... ...
第13页
But When Service requirement
Grows Up
Monday, June 23, 14
第14页
Structure front Server
send Request
Too Much Code To Handle
Internal server
... ...
Monday, June 23, 14
第15页
Service Definition
• Front server can be flexible to connect • Support Restful API for internal connect • Same Router rule for client • Modules install / uninstall be easy • Light way feedback data
Monday, June 23, 14
第16页
Server Definenation
Tips
• •
FSurSopnpetofresretvarRviteceusretfcruaelnaAsbPaeI saffloeraxmiinbtaloeeprtndopaulcscolonennn,eecctt
• hStatmpse://Rgiothuutebr.croumle/cfolorncnl/iemnotdule-loader • Modules install / uninstall be easy • Light way feedback data
Monday, June 23, 14
第17页
app1 app2
app3
app4
Routers as apps, auto route
Monday, June 23, 14
第18页
app1 app2
app3
app4
Every developers can handle by themselves
Monday, June 23, 14
第19页
Make connect easy, A tiny distribution server
Monday, June 23, 14
第20页
Face Response Issue ...
Monday, June 23, 14
第21页
Wait ...Wait ...Wait ...
Monday, June 23, 14
https://www.flickr.com/photos/tormodspictures/
第22页
Monday, June 23, 14
http://chinese.engadget.com/2014/06/21/facebook-android-update-africa/
第23页
Let’s refactoring code
Monday, June 23, 14
第24页
Monday, June 23, 14
第25页
Response back as soon as posible
Monday, June 23, 14
第26页
Heavy processing or request, put late
Monday, June 23, 14
第27页
Response as short as posible
Monday, June 23, 14
https://www.flickr.com/photos/calliope/
第28页
what can NOT be late
• real time date, have to be light (geo, data) • robust data, we could not avoid (but save in
cached, redis and process them later)
Monday, June 23, 14
第29页
What can late
• log data writing • data saving (not for real time response) • data caching
Monday, June 23, 14
第30页
Tips
Response First, Process Later
Monday, June 23, 14
第31页
Too many callback event in waiting queue
Monday, June 23, 14
https://www.flickr.com/photos/peter_curb/14318113011/sizes/c/in/photostream/
第32页
WARNNING, It Cause High CPU Usage
Monday, June 23, 14
https://www.flickr.com/photos/peter_curb/14318113011/sizes/c/in/photostream/
第33页
Monday, June 23, 14
We put process in
Task Queue.
http://learnboost.github.io/kue/
第34页
Save process, prarams in Task Queue
Monday, June 23, 14
第35页
Monday, June 23, 14
第36页
Monday, June 23, 14
Set callback event when things done
第37页
Send request to 3rd party library / API / URL
Monday, June 23, 14
第38页
Feedback to Server or Storage trigger callback event to Server
Monday, June 23, 14
第39页
Monday, June 23, 14
第40页
How about Client?
How feedback to clients, when task queue process task / jobs done, how could we notify clients?
Monday, June 23, 14
第41页
Time meet
http://socket.io/
or
Socket
Monday, June 23, 14
第42页
Client and Server connect via a Tunnel
Monday, June 23, 14
第43页
Monday, June 23, 14
With handshake
第44页
Structure Clients
Server push
Server
Monday, June 23, 14
第45页
Structure Clients
Server push
Server
From single way to double ways
Monday, June 23, 14
第46页
Structure Clients
JSON
Server push
Server
Ads
Habit
From single way to double ways
Monday, June 23, 14
第47页
Server push, that means
• Ajax mode changed • Browser can do more things • User request is getting more • Server has to afford more currency • Server has opportunity feedback data
Monday, June 23, 14
第48页
Let’s integrate with Socket.io / Socket and
Task Queue
Monday, June 23, 14
第49页
Feedback to Server or Storage
Monday, June 23, 14
第50页
Return data to User in a few minutes
Monday, June 23, 14
第51页
Return data to User in a few seconds
Monday, June 23, 14
第52页
Increase User Experience
Not Make Them Wait
Monday, June 23, 14
第53页
Monday, June 23, 14
第54页
Cache Layer
Monday, June 23, 14
第55页
Monday, June 23, 14
Storage Layer
第56页
Monday, June 23, 14
Part of access
第57页
Monday, June 23, 14
Part of heavy access
第58页
Not finished yet
Monday, June 23, 14
第59页
Monday, June 23, 14
第60页
Monday, June 23, 14
http://letsnode.com/example-of-what-node-is-really-good-at
第61页
Monday, June 23, 14
http://mortarnpistol.com/2011/12/16/big-rigs-over-the-road-racing/
第62页
Monday, June 23, 14
第63页
Monday, June 23, 14
第64页
Rules
• Make events as light as posible • Divide things to tiny • Heavy process throw to another way • Monitor app.js after done • Figure out bottlenecks • Decrease users idle / waiting time as posible
Monday, June 23, 14
第65页
https://www.facebook.com/groups/node.js.tw/ https://www.facebook.com/NodeJS.tw https://github.com/nodejs-tw http://nodejs.tw/ http://2014.jsdc.tw/
Monday, June 23, 14
第66页
https://github.com/nodejs-tw/nodejs-book-beginner-guide
Monday, June 23, 14
第67页
Monday, June 23, 14
@clonncd