Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2013 Intro to Sails (v0.9.8)

2013 Intro to Sails (v0.9.8)

In this talk, I give a brief introduction to Sails.js, a realtime, server-side MVC framework for Node.js. I’ll point out some of the problems that Node.js developers face when trying to build apps from scratch, and show how Sails helps solve them. I’ll also live-code examples of some tasks that are simplified by Sails.js.

Sails is an open-source framework built on top of Express, Socket.io, and other industry-standard node modules. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It’s aim is to make Node.js practical for production applications, by providing a familiar structure for an app’s back-end while remaining unopinionated about the front-end as well as database-agnostic. It’s especially good for creating realtime features, such as chat.

(Updated:)
other topics covered:
+ High-level look at associations in Sails v0.10
+ Plugins (generators, hooks, adapters, blueprints, custom responses)

143a2600e408b5a2edbb00c3631ed5f5?s=128

Mike McNeil

May 16, 2013
Tweet

Transcript

  1. sails.js realtime MVC framework for Node.js

  2. who am i? @mikermcneil

  3. We design and develop javascript apps for enterprise and startup

    customers. i have a startup called balderdash
  4. love at rst sight circa 2011 Node.js...

  5. OPEFKTUSBOTMBUFTTFSWFSDPODFQUTJOUP KBWBTDSJQUUIFMJOHVBGSBODBGPSUIFFWFSZ NBO PVSFWFOUMPPQJTGBTUFSUIBOZPVSUIJOH OBUJWFCJOEJOHTNFBOT/PEFDBOEPQSFUUZ NVDIBOZUIJOH DPSFJTQVSQPTFGVMMZGFBUVSFTQBSTF OQNQVCMJTI OVGGTBJE MPXFSCBSSJFSUP

    FOUSZ • • • • •
  6. route& first* philosophy opt&in* framework* structure “smalltalk* ethics” UML “javascript*

    ethics” servlets mvc spring,* et.*al. rails enterprise* java ruby module system (npm) noSQL (mongo/ redis) async* flow* control from*the* front&end: generators (yeoman) node.js convention* over* configuration event*loop ORM
  7. Can we use Node.js for, like, everything? i wondered...

  8. Can we use Node.js for, like, everything? i wondered... maybe.

  9. Realtime used to be kind of hard XMPP Comet Reverse

    AJAX Long polling Flash sockets WebSockets Server-sent events
  10. A lot easier now

  11. Programming realtime apps was still not trivial. Socket.io and Express

    messages/requests have to be handled independently, which leads to separate code bases for realtime and traditional server code.
  12. Programming realtime apps was still not trivial. Socket.io and Express

    messages/requests have to be handled independently, which leads to separate code bases for realtime and traditional server code. Socket programming is a new paradigm for many modern web developers
  13. Socket.io and Express messages/requests have to be handled independently, which

    leads to separate code bases for realtime and traditional server code. Socket programming is a new paradigm for many modern web developers Questions about scalability Programming realtime apps was still not trivial.
  14. Express wasn’t “structured enough” Authentication has to be rolled from

    scratch
  15. Express wasn’t “structured enough” Authentication has to be rolled from

    scratch No standard, implementation-agnostic method of working with datastores
  16. Express wasn’t “structured enough” Authentication has to be rolled from

    scratch No standard, implementation-agnostic method of working with datastores Low level and free-form -- not a lot of guidelines for teams used to convention-over-con guration frameworks
  17. Lots of different kinds of projects There weren’t really any

    simple and reproducible patterns for structuring complicated Node.js aplications top to bottom
  18. Lots of different kinds of projects There weren’t really any

    simple and reproducible patterns for structuring complicated Node.js aplications top to bottom Could be working with an existing client, or writing a new client for a mobile web browser, an automobile, a toaster, or god knows what
  19. Lots of different kinds of projects There weren’t really any

    simple and reproducible patterns for structuring complicated Node.js aplications top to bottom Could be working with an existing client, or writing a new client for a mobile web browser, an automobile, a toaster, or god knows what Lots of different 3rd party proprietary services to deal with on the back-end -- there was no standard way to integrate new systems
  20. too bad. we went with Node anyway.

  21. so Sails.js was born

  22. MVC structure Sails.js is modeled after the same convention-over-con guration

    philosophy you’re used to from frameworks like Rails, Grails, Symfony, and Zend. Controllers are just Express middleware Views are ejs by default, but you can use jade, etc. We rarely use them since we’re normally making single page apps with client-side templates.
  23. Lightweight ORM Kept querying semantics dead-simple and adapter-agnostic whenever possible

    Pulled the best of Active Record, Hibernate, and Mongoose Made it easy to add purpose-built adapters at the app level
  24. Policies Policies are just more Express middleware They can be

    chained together to “protect” or preprocess requests for controllers E.g. access control, storage quotas, or anything else you’d want to use middleware for
  25. Socket.io Express interpreter Translated incoming socket.io messages into Express requests

    Translated res.send(), res.json(), and res.view() to respond via the socket, as well as allowing for streams Added res.broadcast() and req.join() methods for pubsub Normalized con guration
  26. Blueprints Instead of something like Rails HTML scaffolds, by default,

    when you generate a model and controller, Sails serves an API Built-in search, sort, pagination, and complex queries Authentication/access control can be built on top using policies
  27. Other cool stuff CLI tool REPL Custom adapters Optional server-side

    coffee support Automatic asset bundling (LESS and coffeescript les are compiled, merged with css and js, and injected into the DOM, mini ed in production mode)
  28. BTTPDJBUJPOT • TIPVMEVQHSBEFOPUPOMZ8BUFSMJOF 03. CVUBMTP3&45GVMCMVFQSJOUT BOENPEFMGPDVTFEQVCMJTI TVCTDSJCF QVCTVC NFUIPET •

    DSPTTBEBQUFS • DSPTTDPOOFDUJPO • FGàDJFOUBTQPTTJCMF BMMPXJOHNPSF FGàDJFOUTPMVUJPOTUPCFJNQMFNFOUFE BUUIFBEBQUFSMBZFS Sturday, Jauary 25, 4
  29. XIZNFTTXJUIUIJT • /P42- • TJNQMJDJUZ • QFSGPSNBODF • USBEJUJPOBM42-EBUBCBTFT •

    CVJMUJO"$*% • SFQPSUJOHRVFSJFT
  30. DSPTTBEBQUFS • .Z42-UBCMFT • .POHP%#DPMMFDUJPOT • 1PTUHSF42-UBCMFT • 5XJUUFS"1*3&45SFTPVSDFT •

    FUD
  31. DSPTTDPOOFDUJPO • EJGGFSFOUIPTUT TFSWFST • EJGGFSFOUEBUBCBTFVTFST • OBTUZBOOPZJOHJOUFHSBUJPOTX MFHBDZEBUB FHKPJOUXPEJGGFSFOU.Z42-

    EBUBCBTFTXJUIJODPNQBUJCMF TDIFNBT •
  32. IPXJUXPSLT • XIFOQPTTJCMF VTFOBUJWF PQUJNJ[BUJPOT JFKPJOT BUUIF EBUBCBTFMBZFS • JONFNPSZKPJOTGPSUIFSFTU

    KVTU MJLFZPVXPVMEIBWFUPEPJODPEF • NBJOUBJOTNVMUJQMFDPOOFDUJPOQPPMT
  33. JNQBDUPOBEBQUFST • &YJTUJOH4BJMTBEBQUFST BOEBOZ OFXPOFTZPVXSJUF OPXTVQQPSU BTTPDJBUJPOTPVUPGUIFCPY  XJUIPVUXSJUJOHBOZBEEJUJPOBM DPEF

    • :PVDBOBEEBzKPJO zNFUIPE UIBUXJMMCFVTFEJOTUFBEPG zàOE zGPSSVOOJOHJOUSB EBUBCBTFBTTPDJBUJPOTRVFSJFT
  34. let’s code things now OK

  35. CURRENT STATUS (V0..) Community is growing (almost 5000 stars, over

    400 forks on github, active IRC channel and Google group)
  36. Community is growing (almost 5000 stars, over 400 forks on

    github, active IRC channel and Google group) My company and most of our customers are using Sails in production CURRENT STATUS (V0.9.)
  37. My company and most of our customers are using Sails

    in production Oldest running production Sails app has been up for nearly 2 years CURRENT STATUS (V0..) Community is growing (almost 5000 stars, over 400 forks on github, active IRC channel and Google group)
  38. FYUFOEJOHTBJMT • BEBQUFSJOUFSGBDFTUBOEBSETGPSIPXPVS TFSWFSTUBMLUPEBUBCBTFTBOEXFCTFSWJDFT • CMVFQSJOUJOUFSGBDFTUBOEBSETGPSIPXPVS TFSWFSTPGGFSVQUIFJSPXO"1*TUBSUXJUI DPOWFOUJPOBMTFUVQ UIFOKVNQJOUPXSJUJOH DVTUPNDPEFMBUFS

    HFOFSBUPSTQMVHJOTUIBUBMMPX•ƒ‹Ž•‡™BOE •ƒ‹Ž•‰‡‡”ƒ–‡PVUQVUUPCFDPNQMFUFMZ PWFSSJEEFOBOEPSFYUFOEFE • IPPLTQMVHJOTUIBUBMMPXDPNQPOFOUT PGUIF4BJMTSVOUJNFUPCFPWFSSJEEFOPS FYUFOEFE •
  39. BEBQUFST • TBJMTNZTRM ſpm$install$sails*mysql) • TBJMTSFEJT ſpm$install$sails*redis) • TBJMTNPOHP ſpm$install$sails*mongo)

    • TBJMTQPTUHSFTRM ſpm$install$sails*postgresql) • TBJMTSFTU ſpm$install$sails*rest) • TBJMTNBOESJMM ſpm$install$sails*mandrill) • FUD
  40. CMVFQSJOUT • HFOFSJDJNQMGPS)551TPDLFU"1*T • QSJPSJUJ[FTVTBCJMJUZQSBDUJDBMJUZPWFSFMFHBODF • EFDMBSBUJWFDPOàHVSBUJPO FHFNCFSFYQFDUTBDFSUBJOGPSNBU PS BOHVMBSQSFGFSTBDFSUBJO+40/1QBSBNFUFS

    • PWFSSJEBCMF DBOSFQMBDFMPHJDDPNQMFUFMZXJUI JNQFSBUJWFDPEFBUBNPNFOUTOPUJDF • JOTUBMMFEWJBHFOFSBUPSTGPSOPX npm$install$sails*generate*blueprints*ember
  41. HFOFSBUPST • TBJMTHFOFSBUFOFX npm$install$sails*generate*new npm$install$sails*generate*new*coffee npm$install$sails*generate*new*but*like*express npm$install$sails*generate*new*blog • TBJMTHFOFSBUFCBDLFOE npm$install$sails*generate*backend*coffee

    npm$install$sails*generate*new*jade • TBJMTHFOFSBUFGSPOUFOE • TBJMTHFOFSBUFHSVOUàMF • TBJMTHFOFSBUFDPOUSPMMFS npm$install$sails*generate*controller*coffee • TBJMTHFOFSBUFBQJ • FUD
  42. IPPLT r IUUQ r TPDLFUT r PSN r DPST r

    DTSG r NJHSBUJPOT r FUD
  43. Want to get involved? Contribute to an adapter

  44. Want to get involved? Contribute to an adapter improve the

    documentation
  45. Want to get involved? Contribute to an adapter improve the

    documentation Write tutorials
  46. Want to get involved? Contribute to an adapter improve the

    documentation Write tutorials Blog about Sails
  47. Contribute to an adapter improve the documentation Write tutorials Blog

    about Sails Play with Sails and come up with more cool shit Want to get involved?
  48. Questions?

  49. Thanks! @mikermcneil mike@balderdash.co