• Like
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
621
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
20
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Node.js &Mobile Apps Richard Rodger @rjrodger nearform.com
  • 2. •Why should you use Node.js?•Node.js basics•Lessons from a real world project
  • 3. nodejs.org• Code in JavaScript on the server• Built using Chrome V8 Engine - so its really fast!• Everything is event-based; there are no threads
  • 4. Node.js Web Servervar http = require(http);var server = http.createServer( function(req, res) { res.writeHead(200, {Content-Type: text/plain}) res.end(Hello Worldn)});server.listen(80, 127.0.0.1);console.log(Web server running at http://127.0.0.1:80);
  • 5. why?
  • 6. there arethreeeras... 1990s 2000s 2010s
  • 7. "keep your job" Node.js is languages the platform * yes I know about tiobe.com
  • 8. how manyconcurrent clients? 90s: 10 00s: 100 10s: 1000 C10K Problem
  • 9. How doyou scale? processes threads events
  • 10. JavaScript: the worlds ugliest language avoid slow, validate forms date ECMA, jQuery marry Node.js, Crockford
  • 11. Wherewill yourapp live? co-lo buy, build, and rack IaaS Amazon EC2 PaaS Heroku
  • 12. DontForget the Webbrowser wars web 2.0 standardise!
  • 13. Mobile Hybrid Native Web HTML5/JS Gotta learn em all*HTML5/JS
  • 14. Mobile Web Apps• Web Pages built using HTML5 features: • Canvas for drawing • In-built Video and Audio • Geolocation • Web Sockets • Local Storage, Local Caching • CSS3 transitions (not really HTML5)• Use JavaScript as main programming language ft.com business• Designed for Touch Interfaces post.ie • Smart phone or Tablet form factors • Don’t use Hover effects, instead convey touch affordance using 3D styling• Dynamic single page apps. Changes are made to the HTML rather than loading new pages.
  • 15. Mobile Web Standards ComplianceLatest Versions Storage CSS3 Mobile Multimedia local, cache, sql effects, 2D, 3D touch, geo & motion video & audio iOS ★★★ ★★★ ★★★ ★★ Android ★★★ ★★★ ★★★ ★ Win Ph. 7 ★ ★★ ★ ★BlackBerry 6+ ★★ ★★ ★★ ★ Legacy recent Nokias ★ ★ ★ mobilehtml5.org
  • 16. Node.js and Mobile nginx Node.js• nginx • high performance web server • serves static resources: test files, images • proxies requests through to Node.js app server• Node.js • high performance server-side JavaScript • Executes business logic and database queries
  • 17. • Runs high-performance server-side JavaScript • open source, sponsored by joyent.com• Uses the Google Chrome V8 engine • just-in-time compilation to machine code • generation garbage collection (like the Java JVM) • creates virtual “classes” to optimise property lookups• Has a well-designed module system for third party code - very effective and simple to use• Your code runs in a single non-blocking JavaScript thread• That’s OK, most of the time you’re waiting for database or network events
  • 18. being event-driven is likehaving a butler run yourserver
  • 19. Your code runs in one thread only!
  • 20. Working with Events var http = require(http); var server = http.createServer(); server.on( request, function(req, res) { res.writeHead(200, {Content-Type: text/plain}) var echo = req.on(data,function(chunk){ echo += chunk }) req.on(end, function(){ res.end( echo+n ) }) req.on(error, function(err){ res.end( "error: "+err ) }) }) server.listen(80, 127.0.0.1); console.log(try: curl http://127.0.0.1 -d hello);
  • 21. What does the core API give you? Q basics: file system, ... 9 control: events, streams, buffers... g networking: sockets, DNS, ... „ web server: HTTP handling, ...
  • 22. Node.js modules are so cool• Easy syntax • var moduleAPI = require("modulename");• Central repository • npmjs.org• Easy command line install • npm install modulename• No version conflicts! • intelligent dependency management• Declarative project description • package.json file goes into project root folder
  • 23. Some important modules(find them on npmjs.org or github.com)• connect • HTTP middleware infrastructure - requests pass through layers• express • JSP/ASP style dynamic server-side pages• underscore • same as client-side library! provides functional utilities: map, reduce, ...• socket.io • HTML5 real-time web sockets that "just work" - includes client-side API• request • easy outbound calls to web services
  • 24. Node.js is It doesntoverhyped matter • Not a clear winner against • Chrome V8 Engine means Node.js is "Good Enough" other event servers on speed • Asynchronous code is harder • JavaScript means almost all libraries are asynchronous, unlike than synchronous code other event servers • No, you wont re-use much • JavaScript is a local maximum and code between client and youre stuck with it server • Same language on client and server makes your brain happy • Memory can still leak • Compile to JavaScript if you really • Its not a mature platform hate the language
  • 25. businesspost.ie
  • 26. Web Mobile Cloud ServicesWeb Apps Services API Mobile & REST & DatabaseTablet Web JSON Mobile & Horizontal Third PartyTablet Apps Scale Services Desktop Cloud Monitoring Web Hosted
  • 27. Client-side Router #! URLs • Common code-base • even for hybrid apps! Models Data, biz logic • backbone.js • shims for weak browsers Views DOM Layout • browser-targeting: user- agent & capabilities • responsive layout (mostly) Helpers Shared code
  • 28. Server-side map /api/ URLs • nginx & Node.js Router to functions • Small code volume API function( req, • Third party modules: functions res ) { ... } • connect Shared code • express Helpers (some with client) • seneca (my db layer) Open source • Deploy with: Modules heavy-lifting • sudo killall node
  • 29. Cloud ServicesDatabase Hosting MonitorsMongoDB Amazon Amazon Load Redis cloudkick Balancer Instancememcached Continuous Scaling
  • 30. Third Party IntegrationJSON, XML, simple form data, text files, ... ... all easy using JavaScript and Node.js Modules Analytics Twitter E-Commerce In-App Logging Facebook Purchasing Email LinkedIn Stock Feed
  • 31. Native AppsSame code as mobile web versions, ... ... wrapped using PhoneGap to run natively ... plus some native plugins
  • 32. Lesson:
  • 33. Lesson: code volume43210 Client JavaScript Server JavaScript
  • 34. Lesson:multi-platform client-side JavaScript is really hard • a framework is a must • code against ECMA, use shims to support older browsers • backbone.js • Code/Test/Debug inside Safari • business logic must be in • phonegap.github.com/weinre common code for hard to reach places • browser-specific code • use error capture in production • virtual .js files • Finally, use a simple static site as a fallback (also for Googlebot) • use jshint to keep IE happy
  • 35. Lesson:multi-platform HTML/CSS is really hard • "structured" CSS is a must • Clean, semantic HTML is not optional • sass or less • graceful degradation may • Be happy with require radically different CSS • media queries • 100% "Responsive" design is tough • CSS3 transforms • Responsive within browser subsets has higher reward/ • browser-specific code effort • virtual .css files
  • 36. Lesson:the app stores are not web sites • that bug in version 1... • you cant deploy hot fixes • will take two weeks to • make everything fix via an update configurable! • some users will never • All prices, text, host update names, urls, ... • appears after an OS • On launch, app "checks-in" update for new configuration • this will save your life
  • 37. Lesson:Node.js does what it says on the tin • High performance • callback spaghetti is not a problem in practice • High throughput • Low CPU usage • use functional style • Constant memory usage • client-side code is far more difficult • leaks will kill, but then • Dont do CPU intensive stuff • < 100ms startup time • ... theres a warning on • means you may not the tin! notice!
  • 38. Lesson:Outsource your database • Remote MongoDB • Big productivity gain hosting • no production tuning • mongohq.com • no configuration • No downtime • no cluster set up • Backups • Low latency (in Amazon) • Web-based admin (if lazy)
  • 39. Node.js means Rapid Development * Bruno Fernandez-Ruiz http://www.olympum.com/architecture/the- nodejs-innovation-advantage/
  • 40. My CompanyMobile Apps + Node.jsMy Bookrichardrodger.comRichard Rodger@rjrodgernearform.com