Top Banner
33

Developing realtime apps with Drupal and NodeJS

May 10, 2015

Download

Technology

drupalcampest

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Developing realtime apps with Drupal and NodeJS
Page 2: Developing realtime apps with Drupal and NodeJS

About myself

Ivo NellisCTO, Fenomen Veebiagentuur ● Working with Drupal since 2007● Contributed mostly to Estonian translations● Main focus: Drupal, Node.js, MongoDB

Skype: ivonellisTwitter: ivonellis

[email protected]

Page 3: Developing realtime apps with Drupal and NodeJS

Developing realtime apps with Node.js and Drupal

Ivo NellisFenomen veebiagentuur

Page 4: Developing realtime apps with Drupal and NodeJS

With Node.js, you can write web applications

Perfect for fast, scalable, light-weight data-intensive and real-time web applications

Page 5: Developing realtime apps with Drupal and NodeJS

Node.js project is new Relatively...

Page 6: Developing realtime apps with Drupal and NodeJS

Created 2009 by Ryan Dahl

Perl

Python

PHP

Ruby on Rails

Node.js

1990 2000 2010

ASP.NET

Java

Page 7: Developing realtime apps with Drupal and NodeJS

With node, you can write in JavaScript

or in something that compiles to JS(CoffeeScript, ...)

Page 8: Developing realtime apps with Drupal and NodeJS

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.

● It's fast ... and it's getting faster every day○ Huge competition between browser vendors

Page 9: Developing realtime apps with Drupal and NodeJS

But SSJS has been done before..

Jaxer Rhino RingoEjscript Spludo LiveWire

Page 10: Developing realtime apps with Drupal and NodeJS

Node.js is built on Google V8 JS engine

Fast, Single runtime = less compability issues

Page 11: Developing realtime apps with Drupal and NodeJS

Node.js is asynchronous & event driven

and it's great for real-time apps

Page 12: Developing realtime apps with Drupal and NodeJS

Apache request lifecycle

Child processes / threads

Incoming requests

Page 13: Developing realtime apps with Drupal and NodeJS

The cost of I/O

L1-cache 3 cyclesL2-cache 14 cyclesRAM 250 cyclesDisk 41 000 000 cyclesNetwork 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

Page 14: Developing realtime apps with Drupal and NodeJS

Node.js event loop

Single process

Incoming requests

I/O request

I/O callback

Page 15: Developing realtime apps with Drupal and NodeJS

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 email● No templating system● No MVC or framework layer

Page 16: Developing realtime apps with Drupal and NodeJS

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 express

var express = require("express");

Page 17: Developing realtime apps with Drupal and NodeJS

expressconnect socket.io redis

mongodb-native

underscore

async

requestjquery

calipso

nodemailer

https://github.com/joyent/node/wiki/Modules

search.npmjs.org

github.com

Page 18: Developing realtime apps with Drupal and NodeJS

Node.js vs Drupal

Node.js

Apache

Drupal

PHP

Web Server

Scripting language

Connect.js

Express.jsFramework

CMS

Calipso / etc...

Modules, themes, ... Libraries ...

LIB

Page 19: Developing realtime apps with Drupal and NodeJS

Web socketsand socket.io

Page 20: Developing realtime apps with Drupal and NodeJS

Realtime communication over HTTP is difficult

HTTP is request - response by natureNo good solutions (ajax, long polling)

Page 21: Developing realtime apps with Drupal and NodeJS

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/

Page 22: Developing realtime apps with Drupal and NodeJS

Socket.io client & server<script src="/socket.io/socket.io.js"></script><script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); });</script>-------------------------------------------------var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' });});

Page 23: Developing realtime apps with Drupal and NodeJS

Combining Drupal, Node.js and Web sockets

When and how?

Page 24: Developing realtime apps with Drupal and NodeJS

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...

Page 25: Developing realtime apps with Drupal and NodeJS

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 socket.io support○ provides a drupal module that integrates with it○ focuses mainly on realtime updates

http://drupal.org/project/nodejs

Page 26: Developing realtime apps with Drupal and NodeJS

nodejs module for Drupal

Page 27: Developing realtime apps with Drupal and NodeJS

Enables realtime communication

Drupal Client

Node.js & socket.io

Clients

Provides a server script

Provides a module

Page 28: Developing realtime apps with Drupal and NodeJS

Broadcast messages

Page 29: Developing realtime apps with Drupal and NodeJS

Update Watchdog page realtime

Page 30: Developing realtime apps with Drupal and NodeJS

Your custom module $message = (object) array( 'broadcast' => TRUE, 'data' => (object) array( 'subject' => 'Hi!', 'body' => 'An important message!', ), 'channel' => 'my_channel', ); nodejs_enqueue_message($message);

Page 31: Developing realtime apps with Drupal and NodeJS

And the client side implementationDrupal.nodejs.callbacks.example = { callback: function(message) { if (message.channel == "my_channel") { alert(message.data); } }}

Page 32: Developing realtime apps with Drupal and NodeJS

Thank you!

Page 33: Developing realtime apps with Drupal and NodeJS