Architecting large Node.js applications
-
Upload
sergi-mansilla -
Category
Technology
-
view
22.626 -
download
3
description
Transcript of Architecting large Node.js applications
![Page 1: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/1.jpg)
largeArchitecting
node.jsapplications
Monday, October 22, 12
![Page 2: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/2.jpg)
@sergimansilla
Monday, October 22, 12
![Page 3: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/3.jpg)
P r o g r a m
☐ C l o u d 9 I D E ?
☐ G r o w i n g p a i n s
☐ I n t r o d u c i n g A r c h i te c t
☐ Le s s o n s l e a r n e d
Monday, October 22, 12
![Page 4: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/4.jpg)
Normal developers
Monday, October 22, 12
![Page 5: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/5.jpg)
JavaScript Developer
Monday, October 22, 12
![Page 7: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/7.jpg)
debuggingReal
Monday, October 22, 12
![Page 8: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/8.jpg)
(Smart!)Code completion
Monday, October 22, 12
![Page 9: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/9.jpg)
analysisStatic
Monday, October 22, 12
![Page 10: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/10.jpg)
FreeLinux VM
Monday, October 22, 12
![Page 11: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/11.jpg)
Bring your own machineMonday, October 22, 12
![Page 12: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/12.jpg)
Real terminal
Monday, October 22, 12
![Page 13: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/13.jpg)
Collaboration
Monday, October 22, 12
![Page 14: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/14.jpg)
See each other typeDebug togetherProductivity++
Monday, October 22, 12
![Page 15: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/15.jpg)
Create
Share Test
Deploy Run/Debug
Monday, October 22, 12
![Page 16: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/16.jpg)
P r o g r a m
☑ C l o u d 9 I D E ?
☐ G r o w i n g p a i n s
☐ I n t r o d u c i n g A r c h i te c t
☐ Le s s o n s l e a r n e d
Monday, October 22, 12
![Page 17: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/17.jpg)
10,000s
of JavaScriptLOC
Monday, October 22, 12
![Page 18: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/18.jpg)
dynamic,typedweakly
languageMonday, October 22, 12
![Page 19: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/19.jpg)
onethreadsingle
Monday, October 22, 12
![Page 20: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/20.jpg)
Remote VM
Cloud9 datacenter
IDE server Remote VM
Remote VM
Openshift
Monday, October 22, 12
![Page 21: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/21.jpg)
Pure madness
Monday, October 22, 12
![Page 22: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/22.jpg)
ModularizationMonday, October 22, 12
![Page 23: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/23.jpg)
Blackboxcoding
Monday, October 22, 12
![Page 24: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/24.jpg)
Monday, October 22, 12
![Page 25: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/25.jpg)
Java/.NET
Node.js
import(s)
require
Monday, October 22, 12
![Page 26: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/26.jpg)
abstractingawayGreat for
require
Monday, October 22, 12
![Page 27: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/27.jpg)
applicationmodularity
Not Great forrequ
ire
Monday, October 22, 12
![Page 28: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/28.jpg)
duplicated modulesmaps to folder names
Relies on FSrequ
ire
Hard to configure module
Monday, October 22, 12
![Page 29: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/29.jpg)
Dependency error handling
coding time compile time run time
KA-BO
OM!
- Server crash- Unhappy customers- Developer gets fired
Monday, October 22, 12
![Page 30: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/30.jpg)
How to fix it?
Static dependency list
Named services
Easy configuration
Resolve at startup
No FS required
Pass an object
Monday, October 22, 12
![Page 31: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/31.jpg)
P r o g r a m
☑ C l o u d 9 I D E ?
☑ G r o w i n g p a i n s
☐ I n t r o d u c i n g A r c h i te c t
☐ Le s s o n s l e a r n e d
Monday, October 22, 12
![Page 32: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/32.jpg)
Architect
github.com/c9/architect
Monday, October 22, 12
![Page 33: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/33.jpg)
Architect
Everything is a plugin
Plugins can consume plugins
An application is just a set of plugins
Monday, October 22, 12
![Page 34: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/34.jpg)
Dependency model
require
Architect
runtime
compile time
Monday, October 22, 12
![Page 35: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/35.jpg)
Dependency error handlingArchitect
coding time compile time run time
- Happy customers- Developer keeps job- Fails before release
Monday, October 22, 12
![Page 36: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/36.jpg)
Monday, October 22, 12
![Page 37: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/37.jpg)
Declare entity ‘presenter’ with behaviorUse ‘presenter’ to do a presentation
Monday, October 22, 12
![Page 38: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/38.jpg)
Presenter Presentation
Dependency model
Monday, October 22, 12
![Page 39: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/39.jpg)
Express our model
package.json
Builds dependency tree
executing codewithout
Monday, October 22, 12
![Page 40: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/40.jpg)
Monday, October 22, 12
![Page 41: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/41.jpg)
What’s next?
Extract the codeWrap in Architect plugin code
Make two plugins
Monday, October 22, 12
![Page 42: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/42.jpg)
Function signature
Call when done
Monday, October 22, 12
![Page 43: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/43.jpg)
Architect plugin code
Module.exportsOptions - we’ll get to thatImports - everything you ‘consume’Register - invoke when done
Monday, October 22, 12
![Page 44: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/44.jpg)
Monday, October 22, 12
![Page 45: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/45.jpg)
Monday, October 22, 12
![Page 46: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/46.jpg)
Easy to testMock dependencies
Monday, October 22, 12
![Page 47: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/47.jpg)
Assert ‘dance’ is called 11 times
Monday, October 22, 12
![Page 48: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/48.jpg)
No black magic
Specify dependency modelFeed architect a config file
Call ‘createApp’
Monday, October 22, 12
![Page 49: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/49.jpg)
Monday, October 22, 12
![Page 50: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/50.jpg)
Monday, October 22, 12
![Page 51: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/51.jpg)
Configuration
Per-plugin optionsNo global options objectSpecify in config file
Monday, October 22, 12
![Page 52: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/52.jpg)
Monday, October 22, 12
![Page 53: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/53.jpg)
Monday, October 22, 12
![Page 54: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/54.jpg)
Options
Automatically passed in at startupOptions are also dependencies
Fail if options aren’t present
Monday, October 22, 12
![Page 55: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/55.jpg)
Monday, October 22, 12
![Page 56: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/56.jpg)
Monday, October 22, 12
![Page 57: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/57.jpg)
Architect makes you think of your app as chunks of functionality
rather than sets of classes
Monday, October 22, 12
![Page 58: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/58.jpg)
Think ‘chunks of functionality’
Implicit type constraintsKeep implementation private
Swap features instead of interfaces
Monday, October 22, 12
![Page 59: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/59.jpg)
How does Cloud9 use it?
Open source version
Local version (OS + sync)
Hosted version
Normal
FTP
SSH
Monday, October 22, 12
![Page 60: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/60.jpg)
Monday, October 22, 12
![Page 61: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/61.jpg)
Swap feature per implementation
On Open source: talk local filesystem
On FTP: talk FTP library
On SSH: talk via a SSH bridge
Monday, October 22, 12
![Page 62: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/62.jpg)
Here is something your DI framework can’t do
Monday, October 22, 12
![Page 63: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/63.jpg)
IDE instance(FTP)
IDE instance(SSH)
IDE instance(Normal)
IDE instance(Normal)
Single node.js process
Other code (dashboard etc.)
Monday, October 22, 12
![Page 64: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/64.jpg)
Monday, October 22, 12
![Page 65: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/65.jpg)
Architect can do
Multiple instances of same pluginRun independently
But in the same process
Monday, October 22, 12
![Page 66: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/66.jpg)
Monday, October 22, 12
![Page 67: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/67.jpg)
Monday, October 22, 12
![Page 68: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/68.jpg)
HERE’S SOMETHING COOL
Monday, October 22, 12
![Page 69: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/69.jpg)
Centralized eventbus
Loose coupling between plugins
No hard dependencies!
Can also do inter-context communication
Monday, October 22, 12
![Page 70: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/70.jpg)
Eventbus
Plugin
Other plugin
Emit event
React on event
Monday, October 22, 12
![Page 71: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/71.jpg)
Monday, October 22, 12
![Page 72: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/72.jpg)
Monday, October 22, 12
![Page 73: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/73.jpg)
And now scale up
Need something inter-server
Swap it with i.e. Redis PubSub
Plugins will never notice
Modular awesomeness!
Monday, October 22, 12
![Page 74: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/74.jpg)
P r o g r a m
☑ C l o u d 9 I D E ?
☑ G r o w i n g p a i n s
☑ I n t r o d u c i n g A r c h i te c t
☐ Le s s o n s l e a r n e d
Monday, October 22, 12
![Page 75: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/75.jpg)
Modularize in feature blocks
Don’t over engineerDon’t create too small blocksThey aren’t interfaces!
Monday, October 22, 12
![Page 76: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/76.jpg)
Use dependency injection
Architect (JavaScript)StructureMap (.NET)Spring (Java)
Monday, October 22, 12
![Page 77: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/77.jpg)
Avoid context switching
Less code!Less errors!Less boilerplate!
Monday, October 22, 12
![Page 78: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/78.jpg)
Loose coupling
Use an event bus
Smaller dependency graph
Monday, October 22, 12
![Page 80: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/80.jpg)
Happy coding!
Monday, October 22, 12
![Page 81: Architecting large Node.js applications](https://reader033.fdocuments.us/reader033/viewer/2022052619/5550d449b4c905f2318b4ffc/html5/thumbnails/81.jpg)
Sergi Mansilla@sergimansilla
github.com/sergi
http://c9.io
Monday, October 22, 12