Some title, right?
Node.js module for Drupal is so great and a time saver, I’ve got spare time to blog about how to set it up. Future me, we’ll thank me for doing so.
First, let’s define the goals for this blog post
- Learn how to setup the Node.js module and server on a local server
- Setup on a remote server using Heroku and Pantheon
Install node
Node.js module’s README.txt explains how to do it. I’m using Mac so for me it was a simple brew install node
.
Get the latest release of the Node.js module, and enable it.
Download the node libraries needed by executing npm install
from the Node.js module directory. That command will look at package.json
and install all the dependencies.
Ok, On to the interesting parts.
Local Server
Node.js module requires a simple configuration file that will tell it where the node.js server is running, and where the “backend” is (i.e. Drupal). Ignore the example in README.txt, eyes over here please.
Assuming my Drupal site is on http://localhost/d7_dev here’s nodejs.config.js
:
settings = {
scheme: 'http',
port: 5000,
host: 'localhost',
resource: '/socket.io',
serviceKey: 'beejeebusRocks',
backend: {
port: 80,
host: 'localhost',
scheme: 'http',
basePath: '/d7_dev',
messagePath: '/nodejs/message'
},
transports: ['polling', 'websocket'],
debug: true
};
The first part tells Node.js module where the node.js server will live. In the above example it will be http://localhost:5000
. Same host different ports.
The “backend” part tells the node.js server where our Drupal site is.
The serviceKey
is the secret code used by Drupal to communicate with the node.js server. In my example it’s also used as a special thank you for beejeebus, the module maintainer.
Next, from the command line execute node server.js
. If you defined the URL and port correctly (pay extra attention to all those leading and trailing slashes!) you should see
Started http server.
info - socket.io started
Visit http://localhost:5000/
and the (confusing) message “Not found” should greet you. Wait, What? Why “Not found”, if it’s working?!
The reason is that the node.js server is accepting only POST and any GET results with a 404.
Back to Drupal. In your settings.php
add $conf['nodejs_service_key'] = 'beejeebusRocks';
, which is the Drupal equivalent for the serviceKey in the JS configuration.
Visit admin/config/nodejs/config
and fill out the form, this time to let _Drupal know about the node.js server. Again, pay attention to leading and trailing slashes.
Node.js server host = localhost
port = 5000
The Fun Part!
- Enable "Nodejs Notifications" module
- Open another browser in the background, and login as another user
- Back as the administrator go to ``admin/config/nodejs/nodejs_notify/broadcast`` and broadcast your message
- The message will appear using Growl in the second browser
- Repeat 2 - 5 until you climax. Personally, It took me about 10 browsers open simultaneously to get there!
Deploy it!
Ok, we had our fun on the local server time to see it in action, on a _real server. This time let's start with the Drupal part. Probably the quickest way to get it up is to grab an account on Pantheon and create a Clean Drupal 7 installation (I did it two years ago, and haven't regretted since...) Let's say my new site is called ```dev.gizra.gotpantheon.com``` Add the module, and the service key in settings.php, yada yada yada. Indeed, we will still need to deal with the Node.js module configuration. Right after the Heroku section.The Heroku section
Wow. Seriously. Those guys in Heroku made deployment of a node.js server really easy. After you sign up, and download the Heroku toolbelt (a command line utility), you can read this quick introduction that will show you how easy it's to deploy from the command line. If you know how to use git, you'll know to use Heroku.Git clone amitaibu/DrupalNodejsHeroku outside of the Drupal installation.
Quick file overview:
I’ve Copied server.js
from the Node.js module.
I’ve changed a bit the package.json
so Heroku will be able to build it.
I’ve added a default nodejs.config.js
you will need to edit and add your own serviceKey
and backend.host
. Here is how my file looked
settings = {
scheme: 'http',
port: process.env.PORT || 5000,
host: '',
resource: '/socket.io',
serviceKey: 'beejeebusRocks',
backend: {
host: 'dev.gizra.gotpantheon.com',
port: 80,
scheme: 'http',
messagePath: '/nodejs/message'
},
transports: ['xhr-polling'],
debug: true
};
Notice several changes:
- backend.host is now pointing to my own Pantheon site. Remember to change it
- The port of the node.js server is now ``process.env.PORT || 5000``. Heroku will make sure to inject that variable when executed, so our node.js server will listen on the right port
- The transport mechanism is xhr-polling. Heroku still doesn't sup Websockets port, but bare in mind that it's the node.js server that gets the polling, _not the Drupal server
Next, we can deploy it to Heroku.
$ heroku create
$ git push heroku master
(I also added a .gitignore file that will ignore node_modules
in case you will npm install
).
Notice how not only the files are pushed to git, but Heroku now builds the dependencies and launches the server. In the end of the output you will see the URL of your new site. That’s the node.js server URL, Drupal will POST data to. You can also open it with heroku open
Back to Drupal
Visit ``admin/config/nodejs/config`` and fill out the form. _My_ configuration looked like this:Node.js server host = nameless-brook-555.herokuapp.com
port = 80
Enable the Node.js modules, and start enjoying the work of other great people!