Folie 1 - DrupalCon Munich 2012

munich2012.drupal.org

Folie 1 - DrupalCon Munich 2012

August 23rd, 2012

Building real-time systems on top of

Drupal with Node.js

Mikkel Høgh / afd42.com

torsdag den 23. august 12


Design & User Experience 2

Building real-time systems on top of

Drupal with Node.js

Presented by Mikkel Høgh

torsdag den 23. august 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)

torsdag den 23. august 12


4

Drupal is slow

• Hardly controversial

torsdag den 23. august 12


5

PHP doesn’t scale

• Synchronous

• Single threaded

• Shared nothing

torsdag den 23. august 12


6

Synchronous/single threaded

• Resource hogging

• Worst of both worlds

torsdag den 23. august 12


7

Four-lane highway

torsdag den 23. august 12


8

Where synchronous hurts

Running PHP code

Waiting for the database to respond

Waiting for external web services

torsdag den 23. august 12


9

Real-world example

On any given Monday, we will have ~150 Apache processes running,

spending ~15 GB of RAM.

torsdag den 23. august 12


torsdag den 23. august 12

Browser: Please give me page 2 of article 317

Server: Hi. What’s your name? Where do you live? How old are you? What’s your

education? Next of kin? Favorite colour?

Browser: [Answers questions]

- One minute later -

Browser: Please give me page 3 of article 317

Server: Hi. What’s your name? Where do you live? How old are you? What’s your

education? Next of kin? Favorite colour?

Browser: [sighs]

10

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

PHP was never designed for this

• Websockets

• Realtime

• Streaming

• etc.

• PHP is 17 years old – semantically tied to

HTTP anno MCMXCV.

torsdag den 23. august 12


12

Isomorphic code

• The ultimate DRY for web developers

• Share business logic between client and

server

• No more having to implement validation

twice

torsdag den 23. august 12

“By isomorphic we mean that any given line of code (with notable exceptions) can execute

both on the client and the server.”

http://blog.nodejitsu.com/scaling-isomorphic-javascript-code


13

How?

• Database-level integration

• Simple REST

• RPC

• Shared state via Redis

torsdag den 23. august 12


14

Simple REST

• Have the Node.js instance be completely

separate from Drupal.

torsdag den 23. august 12


15

Database-level integration

• 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 }

torsdag den 23. august 12


16

Demo

torsdag den 23. august 12


17

RPC

• DNode on both ends

torsdag den 23. august 12


18

Node.js

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

torsdag den 23. august 12


19

PHP

1


20

Other implementations

• dnode in other languages

• Flotype Bridge

• xmlrpc

torsdag den 23. august 12


21

afd42.com

Expect us.

torsdag den 23. august 12


22

What did you think?

Locate this session on the

DrupalCon Munich website:

http://munich2012.drupal.org/program

Thank you!

torsdag den 23. august 12

More magazines by this user
Similar magazines