Int3c

UX, Design and Product Development Services

Node.js Rules Chatterbox: Customizable Realtime Messages on a Drupal site

This is a the module I wish I had when I started to integrate node.js with Drupal.

In its essence, Node.js Rules Chatterbox is a simple way to make your site spit out real-time messages to your site users.

Whether it's a warning message to admin or notification to certain members, it provides a simple way to customize display messages using Rules. For a site builder, it gives you the real-time notifications power of node.js with the ease of creating customized events and reactions via Drupal Rules UI.

Node.js Rules Chatterbox for Drupal provides a general purpose integration between Drupal and a Node.js backend. Essentially you can configure different events to trigger messages that are subsequently displayed to connected users in realtime.

Drupal doesn’t do realtime. Node.js does. Node.js Rules Chatterbox bridges the gap and creates a flexible system for site builders and developers to display messages in blocks. If you can create a Block and configure a Rule, then you can use this module.

In this post, I’ll to briefly explain how Node.js works (compared to Drupal), then explain how to setup and use Node.js Rules Chatterbox and finally provide a few simple examples and practical use cases.

Node.js and Drupal

Node.js is built for speed and as such, this technology solves one of the major deficiencies of a PHP-based CMS like Drupal or WordPress: fast realtime connections and messaging.

Drupal is a powerful and flexible system for organizing managing content, media and users. The addition of Drupal Commerce turns a Drupal site into one of the best “non cookie cutter” ecommerce solutions available today. Unfortunately, though Drupal might excel at organizing and displaying various types of content, by default it is not the fastest system out there and it was never intended for real-time functionality. In fact, PHP itself is not designed or intended to handle realtime, asynchronous connections.

Enter Node.js for Drupal. This Drupal API module provides an integration between a Drupal site and a realtime node.js backend.

While I often here the the comment from non-Drupal folks about how Drupal is a closed system on PHP, in reality Drupal is a system built to interact with other system. Whether it’s your Apache Solr Server or even a 3rd party service, Drupal is huge mashup of technologies when you need it to be.

Quick Definition: What is Node.js?

Let’s start with a definition. Node.js as defined by Wikipedia is:

Node.js is a software system designed for writing highly scalable Internet applications, notably web servers. Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability.

Let’s break that down a bit in technical terms (if the technical stuff is over your head, skip to next part)

  1. Node.js is javascript that is meant to run on the server (as opposed to traditional usage of javascript which is used to make web pages do fancy stuff).
  2. It’s often coupled with websockets protocol in modern web browsers to all very efficient connections between multiple users.
  3. It’s a thinned down application setup (unlike modern CMS and application setups like Drupal or Ruby on Rails) that focus on the minimal modules according to an applications needs.
  4. It uses Asynchronous I/O (or non-blocking I/O or input/output) processing that allows other processes to continue even before the transmission has completed.

See more at: Drupal Case Study: Using Node.js to Create Live Instant Stock Notifications

If this definition and list of features didn’t confuse you, just focus on the fact that node.js adds non-blocking speed and can provide a way to connect to multiple users efficiently and then push messages to those connected users.

Node.js for Drupal: Installation and Setup

The actual installation of a Node.js and integrating it with Drupal goes a bit beyond this post. Unfortunately, node.js setup can be a pain depending on your server skills. If you are a linux-based server, then the Drupal.org Documentation on Installation will likely work for you.

In my opinion, the easiest way is to use Heroku for your first node.js instance. Just follow this tutorial. Amitai Burstein describes this in detail with code samples in "Drupal, Node.js, Pantheon, and Heroku.” You can see my own post about the Chatroom demo site, which uses this same method.

Once node.js is setup and connected to your Drupal site, then it’s relatively easy to use and integrate one of various contrib modules.

In particularly I’ve used Chatroom module a lot. I’ve even contributed back the Chatroom Sounds module to provide alert sounds when new messages are received. For example on Coghlan Capital, we used a customized version of this module to create a realtime messaging space for financial trades between members and experts. Subscription members receive instant alerts of market changes and recent trades from industry experts.

Arguably the node.js installation is the hardest step and things get easier from here, especially with a module like Node.js Rules Chatterbox.

Node.js Rules Chatterbox: Installation and Initial Setup

Once you have a working node.js server, just enable “Node.js Rules Chatterbox” like a normal module. Then go to the initial configuration page at admin/config/nodejs/rules/channels and "Create Notification Channel Block.”

This is the block that will display messages from configured Rules Reactions to users in real-time. In this configuration, you set the title and channel name as well as the number of messages you wish to display.

Once you've created the channel, you now have a block that you can position and control its visibility like you would any normal block.

If you aren’t familiar with node.js and socket.io, it’s good to take a step back here and explain what’s going on. Essentially, we are creating a special messaging channel that users will connect to when they visit a page. There are number of authentication steps that Drupal’s Node.js API module handles, but once completed the user is part of the channel. In our case, this channel is used to pass messages from Drupal PHP to the node.js backend, then display to the user on their screen.

Once configured make sure the block is visible and accessible on the site according the page or pages you want it displayed.

Using Rules Events and Reactions to Trigger Messages to Node.js Rules Chatterbox

Like Views, Rules module is one of the most important site builder tools modules you should master. If you aren't familar with Rules, check out the free video series Learn Rules Framework from NodeOne. It provides both the fundamentals as well as some useful, lesser known integrations.

For the sake of explanation, let's create a simple Rule to post a message each time a user logins. First, create a Rule with the Event "User has logged in." Then, add a Reaction "Notify to Nodejs Channel." Here are the relevant Rules configurations:

The only really important thing to note is where or which channel you want the message posted to. In our case, we only have one channel created, so we used that one.

It's that simple: *1. Create a Node.js Channel, 2. Configure Block Display, and then 3. Add a Rule to Trigger Messages to be sent to the Block.

Quick Technical Note: Using the API to trigger messages

While this post focused on the site builder, this module also provides a simple API for using code to trigger messages.

First, call nodejs_rules_chatterbox_notify($message,$uid = '') in your module to send a custom notification:

Userid of sender. Default value is logged in user.

Second, implement hook_chatterbox_notify_alter($message) in your module, to modify the notification message before it get published. It's that simple.

Conclusion:

Node.js is one of the best technology choices for fast real-time messaging. Drupal remains one of the best CMSs available and continues to improve. Drupal's Node.js API module makes it simple to integrate node.js backend with Drupal. Several modules are already providing some node.js integration with Drupal and many people are building custom code to mashup Drupal and Node.js.

Node.js Rules Chatterbox fills a gap by making it easy for site builders to use Rules to trigger real-time notifications to their site users.

For example, you could create on the dashboard a real-time notification for new users or newly logged in users. You could create a block that provides messages when new posts or comments are add.

These are just a few examples of how it might be used. What ideas do you have? How might this module be used to solve your site builder needs?

Mark Koester Mark has worked on Drupal since the early days of D6. He is passionate about open source as well as entrepreneurship. When he isn't building, he enjoys traveling and speaking one of his many foreign languages. Chengdu, China

Latest Blog Posts

Ever needed to trim text to fit the height of an area? Shave Module for Drupal helps you do this intelligently by allowing you to truncate text to fit within an html element.

Wouldn’t it be cool if you could use Rules to periodically set, change or modify certain Drupal site configurations and variables?

Yeah, it would be cool and nowit is totally do-able with "Rules Set Site Variables”

Wouldn’t it be cool if you could use Drupal Rules to set, change or modify certain site configurations and variables? Yes, it would be awesome and now it is possible with "Rules Set Site Variables” module.