Node.js integration

Last updated on
4 September 2019

Note: this article only applies to version 8.x-2.x of the Private Message module. Version 8.x-1.x will not work with the Private Message Node.js module.

Overview

By default, the private message module uses 'polling' to check for updates. This means it sends a request to the server every X seconds to check if there are any new messages. This can add a lot of additional overhead to the server, particularly when the user leaves the private message page open and isn't even looking at it. The alternative is to set the ajax refresh time to zero, which disables updates altogether, requiring a page refresh to see if there are any updates.

The Private Message Node.js module provides a solution that updates private message threads without the use of polling, through integration with Node.js, thereby creating the best of both worlds.

Benefits of Node.js integration

  • No ajax polling means less work for the browser.
  • No ajax polling means less work for the server.
  • Message threads immediately show new messages

Requirements

Installation

1. Install Node.js

Instructions on Node.js installation are not provided here, as they differ  per system, and instructions are outside the scope of this document. Please see the Node.js homepage for more information.

2. Download the module with composer

Install the module using composer with the following command:

composer install

3. Install the module with Drupal

Install Private Message Node.js module in Drupal as you would any Drupal module, with all of it's dependencies.

drush en private_message_nodejs

4. Install the Node.js dependendencies

On the command line, navigate to [VENDOR FOLDER]/jaypan/private-message-nodejs and run the following command:

npm install

5. Create the Nodejs configuration folder

Create the [VENDOR FOLDER]/jaypan/config

Create default.json by copying either http-example.default.json (for HTTP connections) or https-example.default.json (for HTTPS connections). Fill in all the values in the JSON file. Note that you will need to navigate to the Private Message settings form, and copy the Nodejs secret value, to paste into default.json. If you are using https, you should first try with port 8443, and if you are able to get that working, you can try other ports.

6. Create the default configuration for the app.

Create [VENDOR FOLDER]/jaypan/config/default.json by copying either ./http-example.default.json (for HTTP connections) or ./https-example.default.json (for HTTPS Connections). Fill in all the values in the JSON file. Note that you will need to navigate to the Private Message settings form in Drupal, and copy the Nodejs secret value, to paste into default.json. If you are using https, you should start with port 8443, and if you are able to get that working, you can try other ports.

7. Start the app

On the command line, navigate to [[VENDOR FOLDER]/jaypan/private-message-nodejs and run the following command:

node app.js

Note: Leave this open and running, as closing the server will stop it from working.

8. Enter the URL to the node.js server in the private message configuration

In the web browser, navigate to /admin/config/private_message/config. Expand thePrivate Message Nodejs settings section, and enter the URL to the node JS app. It should be found at [http or https]://[your domain]:[port number in config/default.json]. Save.

9. Test

In the browser, open the private message page.

In the console, check that there has been some output in the command line where you ran the app. The output will mention things like users joining/leaving channels etc.

Overriding configuration by environment

If you want to have separate configuration per environment, you can do the following.

If your environment is production, then in the [VENDOR FOLDER]/jaypan/private-message-nodejs/config folder, alongside default.json, you can create production.json. Then, to run your app, you would do the following:

export NODE_ENV=production
node app.js

The key here is that NODE_ENV is set to the name of the file without the .json. So you could do a staging server with staging.json and:

export NODE_ENV=staging
node app.js

Help improve this page

Page status: No known problems

You can: