Developing realtime apps with Drupal and NodeJS


Published on

Based on Google's V8 JavaScript engine, NodeJS is a fairly new platform for creating scalable and real-time web applications. I will introduce you to NodeJS internals and ecosystem as well as exaplain why and how you can use Node in your Drupal based projects.

Published in: Technology

Developing realtime apps with Drupal and NodeJS

  1. 1. About myselfIvo NellisCTO, Fenomen Veebiagentuur● Working with Drupal since 2007● Contributed mostly to Estonian translations● Main focus: Drupal, Node.js, MongoDB Skype: ivonellis Twitter: ivonellis
  2. 2. Developing realtime apps with Node.js and Drupal Ivo Nellis Fenomen veebiagentuur
  3. 3. With Node.js, you can write web applicationsPerfect for fast, scalable, light-weight data- intensive and real-time web applications
  4. 4. Node.js project is new Relatively...
  5. 5. 1990 2000 2010 Node.js Ruby on Rails ASP.NET Java PHP PythonPerl Created 2009 by Ryan Dahl
  6. 6. With node, you can write in JavaScript or in something that compiles to JS (CoffeeScript, ...)
  7. 7. Server-side JavaScript is great!● JavaScript is the core of modern Web ○ A lot of existing JS developers ○ Familiarity with asynchronous programming model● Share code between client and server ○ Can use existing libraries on server side as well.● Its fast ... and its getting faster every day ○ Huge competition between browser vendors
  8. 8. But SSJS has been done before.. Jaxer Rhino RingoEjscript Spludo LiveWire
  9. 9. Node.js is built on Google V8 JS engineFast, Single runtime = less compability issues
  10. 10. Node.js is asynchronous & event driven and its great for real-time apps
  11. 11. Apache request lifecycle Incoming requests Child processes / threads
  12. 12. The cost of I/OL1-cache 3 cyclesL2-cache 14 cyclesRAM 250 cyclesDisk 41 000 000 cyclesNetwork 240 000 000 cycles
  13. 13. Node.js event loop Incoming requests I/O callback I/O request Single process
  14. 14. Hello World!var http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);}).listen(8000); ● Node itself does not provide much ● No "standard" webserver features: ● No authentication ● No session handling ● No cookies ● No email ● No templating system ● No MVC or framework layer
  15. 15. You need to extend● Active community● Node package manager (npm) is great● 10913 packages as of 06/2012● Simple one-line installation● Manages all the dependencies for you$ npm install expressvar express = require("express");
  16. 16. express mongodb-nativeconnect redis request nodemailer jquery underscore calipso async
  17. 17. Node.js vs Drupal CMS Modules, themes, ... Libraries ... Calipso / etc... Framework Drupal Express.js LIB Connect.js Scripting language PHP Node.js Web Server Apache
  18. 18. Web sockets and
  19. 19. Realtime communication over HTTP is difficult HTTP is request - response by nature No good solutions (ajax, long polling)
  20. 20. Solution: Web sockets● Websocket API is a part of HTML5 spec● New protocol: ws:// & wss://● Persistant connection● Both parties can send data at any time● Native support in Chrome, Firefox, IE10● With node.js and you can use Web Sockets today
  21. 21. client & server<script src="/"></script><script> var socket = io.connect(http://localhost); socket.on(news, function (data) { console.log(data); });</script>-------------------------------------------------var io = require(;io.sockets.on(connection, function (socket) { socket.emit(news, { hello: world });});
  22. 22. Combining Drupal, Node. js and Web sockets When and how?
  23. 23. When should I consider it?● Doing it all on Apache & Drupal becomes too expensive and unscalable ○ Chat, messaging ○ Liveblog ○ Streaming data (logs, etc...) ○ API layer for a mobile app ○ Realtime widgets (sports, stocks) ○ Games ○ etc...
  24. 24. How?● A: Write your own Node server ○ Write your own module code for Drupal ○ Write your own client code● B: Build on the existing Drupal nodejs module ○ provides node.js server with support ○ provides a drupal module that integrates with it ○ focuses mainly on realtime updates
  25. 25. nodejs module for Drupal
  26. 26. Enables realtime communication Provides a module Drupal Client Clients Node.js & Provides a server script
  27. 27. Broadcast messages
  28. 28. Update Watchdog page realtime
  29. 29. Your custom module $message = (object) array( broadcast => TRUE, data => (object) array( subject => Hi!, body => An important message!, ), channel => my_channel, ); nodejs_enqueue_message($message);
  30. 30. And the client side implementationDrupal.nodejs.callbacks.example = { callback: function(message) { if ( == "my_channel") { alert(; } }}
  31. 31. Thank you!