{Node.JS} Common pitfalls ( The story of small Game Development Project )
Agenda
• Event Loop, Non-Blocking I/O, Async Development • Debugging• Lessons learned, lessons forgotten• Born to Scale (Scalability in mind)• Hey going to production!! Oooops ….
Event LoopSingle Thread – one global scope
Async Development – Burn in Hell
Async Development – Callback Hell ResolvingGive the names to your callbacks
Async Development – Callback Hell ResolvingDivide your structure to small functions
Async Development – Callback Hell ResolvingPromises
Debugging
WebStorm IDE Debugger›Node-Inspector module
Lessons learned, lessons forgotten (Development Phase)
• Choose framework you know or used in previous projects• Don't forget to clear the timer once you set it – or the magic will happen in your app• Divide the code to modules and classes – from beginning• Bot is the same as the user – unification for clarity• Don't use multistep CSS3 animation – you will need to have “animation finished” event• Don't start full refactoring – you will loose a time and will through this idea soon• Use class constants instead of magic numbers 0,1,2 …• Put conditions into functions
Born to Scale (Scalability In mind)
• One way – store application state in cache or db Then put the NodeJs behind the reverse proxy like Nginx• Another way – split socket.io connections across servers + use so called “sticky sessions”
Hey going to production!! Oooops ….
• Logging – log more, log early• Forget about debugger• Who will up your server when it's down - “Forever”• Exceptions - don't throw them until you catch them• Load Balancing – Nginx as reverse Proxy• One thread – one CPU core – Use cluster module
Links
http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/http://callbackhell.com/https://github.com/nodejitsu/foreverhttp://nodejs.org/docs/v0.6.0/api/cluster.htmlhttp://blog.keithcirkel.co.uk/load-balancing-node-js/