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

Transforming real-time with Primus

Transforming real-time with Primus

This is the talk that did about the Primus framework. Which changes the way you use, build and maintain real-time applications.

If want to learn more about primus check out: https://github.com/primus/primus

Or #primus on irc.freenode.net

Arnout Kazemier

January 25, 2014
Tweet

More Decks by Arnout Kazemier

Other Decks in Technology

Transcript

  1. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    classified
    information

    View full-size slide

  2. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    classified
    information

    View full-size slide

  3. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014

    View full-size slide

  4. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014

    View full-size slide

  5. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    3rdEden
    !
    3rd-Eden
    !
    arnout.kazemier
    !
    arnoutkazemier

    View full-size slide

  6. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    what is primus
    or who is primus
    did he really create all the transformers? YES

    View full-size slide

  7. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    history
    the origin of primus

    View full-size slide

  8. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    history
    little known fact
    primus started out as wrapper for engine.io

    View full-size slide

  9. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    history
    little known fact
    then it got refactored in to “portal” as one message
    goes in, and comes out on the other side.

    View full-size slide

  10. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    history
    little known fact
    then I released that this project has way to much
    potential to be just another feature in a framework

    View full-size slide

  11. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    use cases
    the envisioned use cases

    View full-size slide

  12. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    use cases
    module authors
    giving your users total feedom

    View full-size slide

  13. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    use cases
    startups
    real-time is hard to get right

    View full-size slide

  14. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    the community

    View full-size slide

  15. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    stars, but growing steady
    star it on http://github.com/primus/primus
    553
    5 developers with commit access
    make 2 great pull requests, and you’ll get full github access
    forks
    which i regularly check for commits that people “forget” to contribute
    43

    View full-size slide

  16. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    the community

    View full-size slide

  17. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    the community
    #primus on irc.freenode.net
    all other questions are currently posted in the github issue tracker

    View full-size slide

  18. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    the community
    github.com/primus/primus
    or if you want to type less
    primus.io

    View full-size slide

  19. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    deep dive in to primus
    exploring some of it’s functionality
    and how can it help you

    View full-size slide

  20. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    learning primus
    super simple stuff

    View full-size slide

  21. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    learning primus
    cd your-awesome-project!
    !
    $ npm install --save primus ws!
    !
    echo "??" !
    echo “profit!”!
    !
    vim index.js
    CODE EXAMPLE: TERMINAL

    View full-size slide

  22. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    learning primus
    'use strict';!
    !
    var Primus = require("primus")!
    , server = require("http").createServer(fn)!
    , primus = new Primus(server, { transformer:"ws" });!
    !
    primus.on("connection", function connection(spark) {!
    console.log("connection received", spark.id);!
    spark.write("ohai");!
    !
    spark.on("data", function data(msg) {!
    console.log("received", msg);!
    });!
    });!
    !
    server.listen(8080);
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  23. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    learning primus
    !
    !<br/>'use strict';!<br/>!<br/>var primus = new Primus("http://localhost:8080");!<br/>!<br/>primus.on("open", function connected() {!<br/>console.log("connection opened");!<br/>primus.write("ohai");!<br/>});!<br/>!<br/>primus.on("data", function data(msg) {!<br/>console.log(“received", msg);!<br/>});!<br/>
    CODE EXAMPLE: PRIMUS CLIENT

    View full-size slide

  24. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    stream compatible
    .pipe() or message streams
    no need to learn a new API, you already know this

    View full-size slide

  25. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    streamcompatible
    primus.on("end", function disconnected() {!
    console.log("connection ended");!
    });!
    !
    primus.end();!
    primus.write();!
    !
    fs.createReadStream(__dirname + '/index.html').pipe(spark, {!
    end: false!
    });
    CODE EXAMPLE: PRIMUS SERVER & CLIENT

    View full-size slide

  26. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    1 line of code
    to switch real-time libraries
    no more rewrites required, automatically compiles the client

    View full-size slide

  27. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    1 line of code
    var primus = new Primus(server, { !
    transformer: “sockjs" // engine.io, socket.io etc!
    });
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  28. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    module.exports = require(“primus/transformer").extend({!
    server: function () {!
    // This is only exposed and ran on the server.!
    },!
    !
    client: function () {!
    // This is stringified end send/stored in the client.!
    // Can be ran on the server, if used through Node.js!
    },!
    !
    // Optional library for the front-end, assumes globals!
    library: fs.readFileSync(__dirname +"./yourclientlib.js")!
    });
    CODE EXAMPLE: PRIMUS SERVER
    1 line of code

    View full-size slide

  29. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    socket.io

    View full-size slide

  30. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    socket.io
    multiple transports
    cross domain
    invested with bugs
    poorly / not maintained / left for dead
    no message order guarantee
    dies behind firewall/virusscanners

    View full-size slide

  31. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    engine.io

    View full-size slide

  32. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported
    engine.io
    supports multiple transports
    cross domain
    upgrade instead of downgrade
    works behind firewalls & virusscanners
    not well tested
    no message order guarantee

    View full-size slide

  33. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    browserchannel

    View full-size slide

  34. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    browserchannel
    multiple transports
    client maintained by google
    message order guaranteed
    works behind firewalls & virusscanners
    not cross domain
    no websocket support
    coffeescript on the server ._.

    View full-size slide

  35. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    sockjs

    View full-size slide

  36. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    sockjs
    multiple transports (tons of them)
    cross domain
    poor error handling
    no query string allowed for connect
    argh, more coffeescript
    connection delay with firewalls

    View full-size slide

  37. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    websockets (ws)

    View full-size slide

  38. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    websockets (ws)
    super damned fast
    supports binary
    cross domain
    i’m the only somewhat maintainer
    no fallbacks or downgrading
    broken by firewalls/virusscanners

    View full-size slide

  39. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    supported frameworks
    there are some minor inconsistencies
    small, but important to know

    View full-size slide

  40. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    custom encoders
    JSON out of the box
    third-party for more

    View full-size slide

  41. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    custom encoders
    var primus = new Primus(server, { !
    parser: "EJSON" // or binary-pack or a third party module!
    });
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  42. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    module.exports = {!
    encoder: function (data, fn) {!
    // encode data to a string.!
    },!
    !
    decoder: function (data, fn) {!
    // decode data to an object!
    },!
    !
    // Optional library for the front-end, assumes globals!
    library: fs.readFileSync(__dirname +"./yourclientlib.js")!
    };
    CODE EXAMPLE: PRIMUS SERVER
    custom encoders

    View full-size slide

  43. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    custom encoders
    primus.transform('incoming', function (packet) {!
    // This would transform all incoming messages to foo;!
    packet.data = 'foo';!
    });!
    !
    primus.transform('outgoing', function (packet) {!
    // This would transform all outgoing messages to foo;!
    packet.data = 'foo';!
    });
    CODE EXAMPLE: PRIMUS SERVER & CLIENT
    message transformers

    View full-size slide

  44. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    reconnect
    that works
    and is seen as core component, not as a feature

    View full-size slide

  45. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    reconnect
    var primus = new Primus("http://localhost:8080", {!
    strategy: "disconnect, online"!
    });
    CODE EXAMPLE: PRIMUS CLIENT

    View full-size slide

  46. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    offline detection
    all messages are queued
    and close/offline events are fired

    View full-size slide

  47. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    broadcast
    two different ways

    View full-size slide

  48. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    broadcast
    primus.write("message"); // send message to all users!
    !
    primus.forEach(function (spark) {!
    // Or iterate over all connections, select the once you!
    // want and only write to those!
    !
    spark.write("message");!
    });
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  49. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    additional fixes
    and hacks

    View full-size slide

  50. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    additional fixes
    and hacks
    broken websocket detection
    JSON encoder fixes
    ESC button fixes
    sockjs header access
    socket.io feature.* avoidance
    engine.io invalid port detection
    sockjs removal of transports
    socket.io memory leak fixes
    engine.io ip address access

    View full-size slide

  51. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    client works on node
    same api, same code on server & client

    View full-size slide

  52. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    client works on node
    var primus = new Primus(server)!
    , Socket = primus.Socket;!
    !
    var client = new Socket(“http://localhost:8080”);!
    !
    // or if you want to connect to a remote server:!
    var primus = require(“primus”).createSocket({/* options */});
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  53. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    written for real apps
    not demo applications
    or development environments

    View full-size slide

  54. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    plugins
    light core, infinite functionality

    View full-size slide

  55. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    plugins
    // The long awaited Socket.IO 1.0 release with Primus:!
    !
    var server = require("http").createServer(fn)!
    , primus = new Primus(server, { transformer:"engine.io" });!
    !
    primus.use(“emitter","primus-emitter")!
    .use(“multiplex”, require(“primus-multiplex”))!
    .use(“primus-emitter”, "primus-rooms");
    CODE EXAMPLE: PRIMUS SERVER

    View full-size slide

  56. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    module.exports = {!
    server: function () {!
    // This is only exposed and ran on the server.!
    },!
    !
    client: function () {!
    // This is stringified end send/stored in the client.!
    // Can be ran on the server, if used through Node.js!
    },!
    !
    // Optional library for the front-end, assumes globals!
    library: fs.readFileSync(__dirname +"./yourclientlib.js")!
    };
    CODE EXAMPLE: PRIMUS SERVER
    plugins

    View full-size slide

  57. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    much much more
    client works in node.js without changes
    authorization handling
    ip address resolving even behind proxies
    pathname exposure
    library generation
    smart server closing which boots connections
    full header access
    access to raw package data
    event based logger

    View full-size slide

  58. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    build with love

    View full-size slide

  59. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    impossibruu
    not convinced? demo time!

    View full-size slide

  60. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014

    questions
    FYI: these are answered in real-time
    ?
    ?
    ?
    ?
    dafuq?
    where can i donate my $$?
    Y U NO IMPLEMENT X?! ?
    ?
    ?
    ?
    ?
    ?

    View full-size slide

  61. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014
    3rdEden
    !
    3rd-Eden
    !
    arnout.kazemier
    !
    arnoutkazemier

    View full-size slide

  62. Arnout Kazemier, founder 3rd-Eden & Observe.it nodejsconf.it 2014

    View full-size slide