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 talk from DrupalCon Munich 2012.

Mikkel Høgh

August 23, 2012
Tweet

More Decks by Mikkel Høgh

Other Decks in Programming

Transcript

  1. August 23rd, 2012 Building real-time systems on top of Drupal

    with Node.js Mikkel Høgh / afd42.com mandag den 29. oktober 12
  2. Building real-time systems on top of Drupal with Node.js Presented

    by Mikkel Høgh Design & User Experience 2 mandag den 29. oktober 12
  3. Why? • 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 • Separation of concerns (CMS vs API) 4 mandag den 29. oktober 12
  4. Where synchronous hurts 9 Running PHP code Waiting for the

    database to respond Waiting for external web services mandag den 29. oktober 12
  5. Real-world example 10 On any given Monday, we will have

    ~150 Apache processes running, spending ~15 GB of RAM. mandag den 29. oktober 12
  6. Shared nothing • Making your server act as it had

    absolute dementia • Works well for old school web pages • Not really good for modern web applications 11 mandag den 29. oktober 12
  7. PHP was never designed for this • Websockets • Realtime

    • Streaming • etc. • PHP is 17 years old – semantically tied to HTTP anno MCMXCV. 12 mandag den 29. oktober 12
  8. Isomorphic code • The ultimate DRY for web developers •

    Share business logic between client and server • No more having to implement validation twice 13 mandag den 29. oktober 12
  9. How? • Database-level integration • Simple REST • RPC •

    Shared state via Redis 14 mandag den 29. oktober 12
  10. Simple REST • Have the Node.js instance be completely separate

    from Drupal. 15 mandag den 29. oktober 12
  11. Database-level integration • npm install drupal 16 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 } mandag den 29. oktober 12
  12. Node.js 19  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); mandag den 29. oktober 12
  13. PHP 20 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!' mandag den 29. oktober 12
  14. What did you think? Locate this session on the DrupalCon

    Munich website: http://munich2012.drupal.org/program Thank you! 23 mandag den 29. oktober 12