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)

Mike McNeil

May 16, 2013
Tweet

More Decks by Mike McNeil

Other Decks in Programming

Transcript

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

    View Slide

  2. who am i?
    @mikermcneil

    View Slide

  3. We design and develop javascript apps for
    enterprise and startup customers.
    i have a startup called balderdash

    View Slide

  4. love at rst sight
    circa 2011
    Node.js...

    View Slide

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






    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  9. Realtime used to
    be kind of hard
    XMPP
    Comet
    Reverse AJAX
    Long polling
    Flash sockets
    WebSockets
    Server-sent events

    View Slide

  10. A lot easier now

    View Slide

  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.

    View Slide

  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

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  20. too bad.
    we went with Node anyway.

    View Slide

  21. so Sails.js was born

    View Slide

  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.

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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)

    View Slide

  28. BTTPDJBUJPOT
    • TIPVMEVQHSBEFOPUPOMZ8BUFSMJOF
    03.
    CVUBMTP3&45GVMCMVFQSJOUT
    BOENPEFMGPDVTFEQVCMJTI
    TVCTDSJCF QVCTVC
    NFUIPET
    • DSPTTBEBQUFS
    • DSPTTDPOOFDUJPO
    • FGàDJFOUBTQPTTJCMF BMMPXJOHNPSF
    FGàDJFOUTPMVUJPOTUPCFJNQMFNFOUFE
    BUUIFBEBQUFSMBZFS
    Sturday, Jauary 25, 4

    View Slide

  29. XIZNFTTXJUIUIJT
    • /P42-
    • TJNQMJDJUZ
    • QFSGPSNBODF
    • USBEJUJPOBM42-EBUBCBTFT
    • CVJMUJO"$*%
    • SFQPSUJOHRVFSJFT

    View Slide

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

    View Slide

  31. DSPTTDPOOFDUJPO
    • EJGGFSFOUIPTUT TFSWFST

    • EJGGFSFOUEBUBCBTFVTFST
    • OBTUZBOOPZJOHJOUFHSBUJPOTX
    MFHBDZEBUB
    FHKPJOUXPEJGGFSFOU.Z42-
    EBUBCBTFTXJUIJODPNQBUJCMF
    TDIFNBT

    View Slide

  32. IPXJUXPSLT
    • XIFOQPTTJCMF VTFOBUJWF
    PQUJNJ[BUJPOT JFKPJOT
    BUUIF
    EBUBCBTFMBZFS
    • JONFNPSZKPJOTGPSUIFSFTU KVTU
    MJLFZPVXPVMEIBWFUPEPJODPEF
    • NBJOUBJOTNVMUJQMFDPOOFDUJPOQPPMT

    View Slide

  33. JNQBDUPOBEBQUFST
    • &YJTUJOH4BJMTBEBQUFST BOEBOZ
    OFXPOFTZPVXSJUF
    OPXTVQQPSU
    BTTPDJBUJPOTPVUPGUIFCPY
    XJUIPVUXSJUJOHBOZBEEJUJPOBM
    DPEF
    • :PVDBOBEEBzKPJO
    zNFUIPE
    UIBUXJMMCFVTFEJOTUFBEPG
    zàOE
    zGPSSVOOJOHJOUSB
    EBUBCBTFBTTPDJBUJPOTRVFSJFT

    View Slide

  34. let’s code things now
    OK

    View Slide

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

    View Slide

  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.)

    View Slide

  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)

    View Slide

  38. FYUFOEJOHTBJMT
    • BEBQUFSJOUFSGBDFTUBOEBSETGPSIPXPVS
    TFSWFSTUBMLUPEBUBCBTFTBOEXFCTFSWJDFT
    • CMVFQSJOUJOUFSGBDFTUBOEBSETGPSIPXPVS
    TFSWFSTPGGFSVQUIFJSPXO"1*TUBSUXJUI
    DPOWFOUJPOBMTFUVQ UIFOKVNQJOUPXSJUJOH
    DVTUPNDPEFMBUFS
    HFOFSBUPSTQMVHJOTUIBUBMMPX•ƒ‹Ž•‡™BOE
    •ƒ‹Ž•‰‡‡”ƒ–‡PVUQVUUPCFDPNQMFUFMZ
    PWFSSJEEFOBOEPSFYUFOEFE
    • IPPLTQMVHJOTUIBUBMMPXDPNQPOFOUT
    PGUIF4BJMTSVOUJNFUPCFPWFSSJEEFOPS
    FYUFOEFE

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  42. IPPLT
    r IUUQ
    r TPDLFUT
    r PSN
    r DPST
    r DTSG
    r NJHSBUJPOT
    r FUD

    View Slide

  43. Want to get involved?
    Contribute to an adapter

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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?

    View Slide

  48. Questions?

    View Slide

  49. Thanks!
    @mikermcneil
    [email protected]

    View Slide