Server-side JavaScript is great! ○ JavaScript is the core of modern Web. ○ A lot
of existing JS developers. ○ Familiarity with asynchronous programming model.
Developing realtime apps with Node.js and Drupal Ivo Nellis Fenomen
Node.JS Web sockets Nodejs module for Drupal
Ivo Nellis CTO, Fenomen ● Small(ish) Drupal shop from Estonia (6 devs) ● One of the early adopters of Drupal (5.x) ● Do stuff only on Drupal Skype: ivonellis Twitter: ivonellis
[email protected]
My Node.js pet project
1990
2000
2010 Node.js
Ruby on Rails
ASP.NET Java
PHP
Python
Perl
Created 2009 by Ryan Dahl
Node.js = web server + scripting language It's very light-weight and fast
With node, you write in JavaScript or in something that compiles to JS (CoffeeScript, ...)
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.
● But there's always someone who hates SSJS.
Whay SSJS hasn't taken off before?
Jaxer Rhino Ringo Ejscript Spludo LiveWire
Node.js is built on Google V8 JS engine Node benefits from the browser "speed race".
Node.js is asynchronous & event driven and it's great for real-time apps
Apache request lifecycle
Incoming requests
Child processes / threads
The cost of I/O L1-cache
3 cycles
L2-cache
14 cycles
RAM
250 cycles
Disk
41 000 000 cycles
Network
240 000 000 cycles http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait
Node.js event loop
Incoming requests
I/O callback
I/O request
Single process
Hello World! var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': text/plain'}); res.end('Hello World\n'); }).listen(8000); ● ● ● ● ● ● ●
Node itself does not provide much No "standard" webserver features: No authentication No session handling No cookies No templating system No MVC or framework layer
You need to extend ● ● ● ● ● ●
Active community Node package manager (npm) is great 10913 packages as of 06/2012 15567 packages as of 10/2012 ! Simple one-line installation Manages all the dependencies for you
$ npm install express var express = require("express");
express connect
mongodb-native
socket.io
request
nodemailer
underscore
calipso
async https://github.com/joyent/node/wiki/Modules search.npmjs.org github.com
redis
jquery
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
Web sockets and socket.io
Realtime communication over HTTP is difficult HTTP is request - response by nature No good solutions (ajax, long polling)
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 socket.io you can use Web Sockets today http://socket.io/
Socket.io client & server var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); }); -----------------------------------------------var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); });
Combining Drupal, Node. js and Web sockets When and how?
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...
How? ● A: Write your own Node server ○ Write your own module code for Drupal ○ Write your own client side code
● B: Build on the existing Drupal nodejs module ○ provides node.js server with socket.io support ○ provides a drupal module that integrates with it http://drupal.org/project/nodejs
nodejs module for Drupal
Enables realtime communication Provides a module
Client Clients
Drupal
Node.js & socket.io Provides a server script
Your custom module $message = (object) array( 'broadcast' => TRUE, 'data' => (object) array( 'subject' => 'Hi!', 'body' => 'An important message!', ), 'channel' => 'my_channel', ); nodejs_enqueue_message($message);
And the client side implementation Drupal.nodejs.callbacks.example = { callback: function(message) { if (message.channel == "my_channel") { alert(message.data); } } }
Demo?