Drupal and Node JS on Ubuntu Localhost

Recently I was trying to get the Node.js integration module running on my localhost with drupal running under a sub-directory and running into a few issues. The main item being that the configuration generation tool in the Node.js integration module did not seem to work as expected. Here are the consolidated set of steps I took to eventually get the 'Broadcast Message' to actually work.

Install Node and NPM [node package manager]

I did follow the instructions on the Node.js integration documentation with one caveat, I installed node and npm separately as it errored with some strange dependency issues.

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

I ended up having to install npm from source using the following (be patient, it might look like it has frozen, but it can take a few minutes to complete):

curl https://npmjs.org/install.sh | sudo sh

You can verify the npm installation by running npm -v.

Install the Node.js integration module into your drupal site

If you're using drush you can just run the following from your Drupal directory:

drush dl nodejs

Otherwise install and enable the Node.js integration module as any other Drupal module. You'll want to enable the following modules once installed:

  • Nodejs integration
  • Nodejs Config
  • Nodejs Notifications

Install required node modules with npm

sudo npm install
sudo npm install socket.io
sudo npm install request
sudo npm install express
sudo npm install connect

Generate your nodejs.config.js file

Go to admin/config/nodejs/config and provide the following:

  • Protocol used by Node.js server: HTTP
  • Node.js server host: localhost
  • Pages on which to enable node.js: *

Then got to admin/config/nodejs/js and provide the following:

  • Host: localhost
  • Port: 5000
  • Resource: /socket.io
  • Publish URL: publish
  • Service Key: [ANY RANDOM STRING]
  • Client write to channels: checked
  • Client write to clients: checked
  • Backend
    • Host: localhost
    • Port: 80
    • Auth Path: /drupal7/nodejs/message/If the front page of your local drupal site is http://localhost/drupal7
  • Debug: checked
  • Extensions: [LEAVE EMPTY]

Once you have all that entered, submit the form. When the page refreshes copy the 'Suggested Configuration' and paste it into a file named 'nodejs.config.js' in your nodejs module directory sites/all/modules/nodejs/nodejs.config.js

Here's a copy of the configuration file for my install:

backendSettings = {
  "scheme":"http",
  "host":"localhost",
  "port":5000,
  "key":"/path/to/key/file",
  "cert":"/path/to/cert/file",
  "resource":"/socket.io",
  "publishUrl":"publish",
  "serviceKey":"SERVICEKEY",
  "backend":{
    "port":80,
    "host":"localhost",
    "messagePath":"/drupal7/nodejs/message/"
  },
  "clientsCanWriteToChannels":true,
  "clientsCanWriteToClients":true,
  "extensions":"",
  "debug":true,
  "transports":[
    "websocket",
    "flashsocket",
    "htmlfile",
    "xhr-polling",
    "jsonp-polling"
  ],
  "jsMinification":true,
  "jsEtag":true,
  "logLevel":1
};

Update your Drupal settings.php

Add the following to the bottom of your settings.php file:
$conf['nodejs_service_key'] = '[SERVICE KEY FROM ABOVE (ANY RANDOM STRING)]';

Start the Node server

From the command line, go to your nodejs module directory and run the following:
node server.js

You should see the following:

Started http server.
   info  - socket.io started

Test it out!

You'll want to open up two browser windows and place them side-by-side so you can see the node.js magic. In one window navigation to admin/config/nodejs/nodejs_notify/broadcast and in the other, go to any other page on the site. On the System-wide broadcast page, fill out the form and hit submit. You should see the message appear in your other browser window.

Troubleshooting

If you don't see the message, try some of the other setups found on the documentation page.

3 comments

by Queen (not verified) on Fri, 02/07/2014 - 21:13

It is very useful. Thanks a lot for sharing.

by Meño (not verified) on Sun, 02/16/2014 - 14:00

Do you have any idea how to fix this? "Backend authentication url not found"
Thanks

by Jaya (not verified) on Wed, 03/09/2016 - 02:03

You may want to check this link,
https://www.drupal.org/node/1944350

Make sure that you set the basePath variable to drupal-base-dir with respect to 'host' value.

Drupal and Node JS on Ubuntu Localhost | Gerrit Brands

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /homepages/29/d196880538/htdocs/drupal7/includes/common.inc:2681) in drupal_send_headers() (line 1212 of /homepages/29/d196880538/htdocs/drupal7/includes/bootstrap.inc).
  • PDOException: SQLSTATE[42000]: Syntax error or access violation: 1142 INSERT command denied to user 'dbo354826752'@'74.208.16.6' for table 'watchdog': INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => Attempting to re-run cron while it is already running. [:db_insert_placeholder_3] => a:0:{} [:db_insert_placeholder_4] => 4 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://www.gerritbrands.com/blog/drupal-and-node-js-ubuntu-localhost [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 54.83.122.227 [:db_insert_placeholder_9] => 1513148444 ) in dblog_watchdog() (line 154 of /homepages/29/d196880538/htdocs/drupal7/modules/dblog/dblog.module).

Error

The website encountered an unexpected error. Please try again later.