Share
Embed
Building real-time systems on top of Drupal with Node.js
My presentation from Drupalhagen 2012.
Mikkel Høgh
October 27, 2012
Transcript
30 slides
-
Building real-time systems on top of Drupal with Node.js Mikkel
Høgh / abl.is Ablis ≫ mandag den 29. oktober 12 -
There will be code on the following slides. A fair
warning mandag den 29. oktober 12 -
• 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 -
• Synchronous • Single threaded • Shared nothing PHP doesn’t
scale mandag den 29. oktober 12 -
• Resource hogging • Worst of both worlds Synchronous/single threaded
mandag den 29. oktober 12 -
Running PHP code Waiting for the database to respond Waiting
for external web services Where synchronous hurts mandag den 29. oktober 12 -
On any given Monday, we will have ~150 Apache processes
running, spending ~15 GB of RAM. Real-world example mandag den 29. oktober 12 -
• 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 -
• 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 -
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 -
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 -
What?! • Tool for building fast network services • Uses
the same JavaScript engine as Google Chrome (V8) mandag den 29. oktober 12 -
• 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 -
Easily 10x faster than PHP/nginx (for what we use it
for, anyways) mandag den 29. oktober 12 -
Simple AJAX responses in ~50 ms. Includes HTTPS, database queries,
etc. mandag den 29. oktober 12 -
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 -
• Database-level integration • Simple REST • RPC • Shared
state via Redis How? mandag den 29. oktober 12 -
Have the Node.js instance be completely separate from Drupal. Simple
REST mandag den 29. oktober 12 -
• 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 -
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 -
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 -
• dnode in other languages • Flotype Bridge • xmlrpc
Other implementations mandag den 29. oktober 12 -
Benefits • Response time • Concurrency • Server-side state in
memory • JavaScript everywhere • Speed mandag den 29. oktober 12