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

Building real-time systems on top of Drupal with Node.js

Building real-time systems on top of Drupal with Node.js

My presentation from Drupalhagen 2012.

2c3b9882e6898958b892a218b5493af9?s=128

Mikkel Høgh

October 27, 2012
Tweet

Transcript

  1. Building real-time systems on top of Drupal with Node.js Mikkel

    Høgh / abl.is Ablis ≫ mandag den 29. oktober 12
  2. There will be code on the following slides. A fair

    warning mandag den 29. oktober 12
  3. mandag den 29. oktober 12

  4. • For some use cases, some or all of the

    following is True: • Drupal is slow • PHP doesn’t scale • PHP was never designed for this • Isomorphic code Why? mandag den 29. oktober 12
  5. • Hardly controversial Drupal is slow mandag den 29. oktober

    12
  6. • Synchronous • Single threaded • Shared nothing PHP doesn’t

    scale mandag den 29. oktober 12
  7. • Resource hogging • Worst of both worlds Synchronous/single threaded

    mandag den 29. oktober 12
  8. Four-lane highway mandag den 29. oktober 12

  9. Running PHP code Waiting for the database to respond Waiting

    for external web services Where synchronous hurts mandag den 29. oktober 12
  10. On any given Monday, we will have ~150 Apache processes

    running, spending ~15 GB of RAM. Real-world example mandag den 29. oktober 12
  11. • Your server acting as it had absolute dementia •

    Works well for old school web pages • Not really good for modern web applications Shared nothing mandag den 29. oktober 12
  12. • Websockets • Realtime • Streaming • etc. • PHP

    is 17 years old – semantically tied to HTTP anno MCMXCV. PHP was never designed for this mandag den 29. oktober 12
  13. Meet node.js 1  var  http  =  require(‘http’); 2  http.createServer(function  (req,

     res)  { 3    res.writeHead(200,  {‘Content-­‐Type’:  ‘text/plain’}); 4    res.end(‘Hello  World\n’); 5  }).listen(1337,  ‘127.0.0.1’); 6  console.log(‘Server  running  at  http://127.0.0.1:1337/’); mandag den 29. oktober 12
  14. What? • Node.js is a platform built on Chrome's JavaScript

    runtime for easily building fast, scalable network applications. • Node.js uses an event-driven, non- blocking I/O model that makes it lightweight and efficient, perfect for data- intensive real-time applications that run across distributed devices. mandag den 29. oktober 12
  15. What?! • Tool for building fast network services • Uses

    the same JavaScript engine as Google Chrome (V8) mandag den 29. oktober 12
  16. • The ultimate DRY for web developers • Share business

    logic between client and server • No more having to implement validation twice Isomorphic code mandag den 29. oktober 12
  17. Speed, speed, speed Benchmarks are meaningless, but… mandag den 29.

    oktober 12
  18. Easily 10x faster than PHP/nginx (for what we use it

    for, anyways) mandag den 29. oktober 12
  19. Simple AJAX responses in ~50 ms. Includes HTTPS, database queries,

    etc. mandag den 29. oktober 12
  20. Stateful server • Everything is so much easier (just stick

    it in an array) • Everything is so much harder (what’s going on here, who moved my cheese?) • No database roundtrips (fast) • No persistence (ouch) mandag den 29. oktober 12
  21. • Database-level integration • Simple REST • RPC • Shared

    state via Redis How? mandag den 29. oktober 12
  22. Have the Node.js instance be completely separate from Drupal. Simple

    REST mandag den 29. oktober 12
  23. • npm install drupal  1  /**  2    *  Validate

     that  the  user  sesion  is  valid  and  matches  the   uid  provided.  3    */  4  function  validate_session(uid,  sid,  callback)  {  5      //  Validate  the  user's  session.  6      drupal.user.session_load(sid,  function  (err,  session)  {  7          //  Load  the  user  object  from  Drupal.  8          drupal.user.load(uid,  function  (err,  user)  {  9              drupal.user.role_permissions(user.roles,  function   (perms)  { 10                  user.permissions  =  perms; 11                  callback(null,  user); 12              }); 13          }); 14      }); 15  } Database-level integration mandag den 29. oktober 12
  24. Demo mandag den 29. oktober 12

  25. • DNode on both ends RPC mandag den 29. oktober

    12
  26.  1  var  dnode  =  require('dnode');  2  var  net  =  require('net');

     3    4  var  server  =  net.createServer(function  (c)  {  5      var  d  =  dnode({  6          transform  :  function  (s,  cb)  {  7              cb(s.replace(/[aeiou]{2,}/,  'oo').toUpperCase())  8          }  9      }); 10      c.pipe(d).pipe(c); 11  }); 12   13  server.listen(5004); Node.js mandag den 29. oktober 12
  27. 1  <?php 2  require_once  "dnode-­‐php-­‐sync-­‐client/DnodeSyncClient.php"; 3  $dnode  =  new  \DnodeSyncClient\Dnode();

    4  $connection  =  $dnode-­‐>connect('localhost',  5004); 5  $response  =  $connection-­‐>call('echo',  array('Hello,  world!')); 6  //  Returns  'Hooloo  woorld!' PHP mandag den 29. oktober 12
  28. • dnode in other languages • Flotype Bridge • xmlrpc

    Other implementations mandag den 29. oktober 12
  29. Benefits • Response time • Concurrency • Server-side state in

    memory • JavaScript everywhere • Speed mandag den 29. oktober 12
  30. https://abl.is/ Expect us. Ablis ≫ mandag den 29. oktober 12