http://adammalone.net | http://drupal.org/user/1295980 http://adammalone.net | http://drupal.org/user/1295980 Drupal and Node.js Push your site
http://adammalone.net | http://drupal.org/user/1295980 http://adammalone.net | http://drupal.org/user/1295980
Drupal and Node.js
Push your site
http://adammalone.net | http://drupal.org/user/1295980
Node.js • Server based JavaScript
• Event driven
• Asynchronous
• Non-blocking
http://adammalone.net | http://drupal.org/user/1295980
http://adammalone.net | http://drupal.org/user/1295980
http://adammalone.net | http://drupal.org/user/1295980
Node.js – more detail • Single Process
o Great for simplicity
o Must be careful not to block the main event loop
• Multiple threads generated
• Event Loop
http://adammalone.net | http://drupal.org/user/1295980
Event Loop
http://adammalone.net | http://drupal.org/user/1295980
node.js Webserver http://nodejs.adammalone.net:13337/ var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Nice and Simple node.js webserver!\n'); }) .listen(13337); console.log('Server running at http://127.0.0.1:1337/');
http://adammalone.net | http://drupal.org/user/1295980
PHP vs Node.js
• Assume I run Reuters/AP
• Necessity for speed in breaking news
• Examples are useful!
http://adammalone.net | http://drupal.org/user/1295980
Drupal Integration • ‘Plumbing’ Module: Nodejs Integration http://drupal.org/project/nodejs
http://adammalone.net | http://drupal.org/user/1295980
project/nodejs • 5 Main hooks/functions
o hook_nodejs_handlers_info o hook_nodejs_user_channels
o nodejs_send_content_channel_token &
nodejs_send_content_channel_message
o nodejs_enqueue_message
http://adammalone.net | http://drupal.org/user/1295980
Simplicity $commands[] = ajax_command_before('#main-wrapper', $output); $message = (object) array( 'channel' => 'chat_nodejs_auth', 'commands' => $commands, 'callback' => 'nodejsExtras', ); nodejs_enqueue_message($message);
(function ($) { Drupal.Nodejs.callbacks.nodejsExtras = { callback: function (message) { Drupal.nodejs_ajax.runCommands(message); } }; }(jQuery));
http://adammalone.net | http://drupal.org/user/1295980
Extending • Drupal
o Nodejs chat module o Nodejs comments o Users online block o Breaking News o Heartbeat o File Downloads o Your module here
• Node o mymodule.server.js o Forever o Many others to hook into
http://adammalone.net | http://drupal.org/user/1295980
Why Not?
• Node.js isn’t for everything
• Do not use for CPU bound operations
• Callback spaghetti
• Lots of rage online
http://adammalone.net | http://drupal.org/user/1295980
Questions [email protected] http://adammalone.net