Little stuck on this, would really appreciate if someone could give me some pointers. I am receiving the following error:

Failed to parse authentication message: [SyntaxError: Unexpected token <]

Here is my nodejs.config.js

settings = {
  scheme: 'http',
  port: 8080,
  host: 'ec2-50-18-64-89.us-west-1.compute.amazonaws.com',
  resource: '/socket.io',
  serviceKey: 'XXXX', // Hidden on purpose
  backend: {
    port: 80,
    host: 'sandbox.cyac.jp',
    scheme: 'http',
    messagePath: '/nodejs/message'
  },
  debug: true,
  extensions: [],
  clientsCanWriteToChannels: false,
  clientsCanWriteToClients: false,
  transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling'],
  jsMinification: true,
  jsEtag: true,
  logLevel: 1
};

Comments

SocialNicheGuru’s picture

I get this too.

stewart.adam’s picture

For those with this problem, it is most likely because your Node.JS server is getting a HTML response when it expects a different type of data stream. To troubleshoot, set 'debug: true' in your nodejs.config.js file and then restart the server. It should print out the URL & response it gets to assist in troubleshooting.

In my case, it was because of the server returning a 401 as it expected HTTP basic auth. I achieved this by adding a header to the options object in server.js:

var options = {
  uri: 'http://foobar',
  body: 'body-text',
  headers: {
    'Authorization': 'Basic ' + new Buffer('username' + ':' + 'password').toString('base64'),
    'Content-Length': Buffer.byteLength(requestBody),
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}

edit: I've patched in support for a 'httpAuth' setting in the backend options for nodejs.config.js - see #1973270: Feature: HTTP authentication support for backend URL

SocialNicheGuru’s picture

Issue summary: View changes

I am uncertain how to use this.
with the settings httpAuth do I need to specify a system wide username/password with certain privileges?

arosboro’s picture

I ended up using restws's restws_basic_auth module, because my whole site redirects unauthenticated requests to /user to login. That should do the trick if you need to authenticate a drupal account with this module.

dynahiol’s picture

@arosboro how did you do that?
I've the restws_basic_auth module installed. But i've no idea how to configure it to. I'm also getting :
authenticateClientCallback: Failed to parse authentication message: [SyntaxError: Unexpected token <]
I user the httpAuth with no positive result. May be because of the logintobogan module redirecting all unauthenticated requests to /user to login.
Can you please provide to steps how you configure the restws_basic_auth module to authenticate a drupal account with this nodejs module?
Thanks.

kplatis’s picture

Hello! I get the same error whenever there is a connection between the a client and the NodeJS server. The message I get in the server debug logging is:

Started http server.
[2016/12/13 11:27:19] authenticateClient: Authenticating client with key ae60dacacde6fd37db2242e4204bc6ff
[2016/12/13 11:27:19] Sending message to backend
[2016/12/13 11:27:19] message
{ authToken: 'ae60dacacde6fd37db2242e4204bc6ff',
messageType: 'authenticate',
clientId: 'weSGU2AD_tsjjEuEAAAA' }
[2016/12/13 11:27:19] options
{ uri: 'http://MYSITE/nodejs/message/',
body: 'messageJson=%7B%22authToken%22%3A%22ae60dacacde6fd37db2242e4204bc6ff%22%2C%22messageType%22%3A%22authenticate%22%2C%22clientId%22%3A%22weSGU2AD_tsjjEuEAAAA%22%7D&serviceKey=MYSERVICEKEY',
headers:
{ 'Content-Length': 193,
'Content-Type': 'application/x-www-form-urlencoded' } }
[2016/12/13 11:27:19] authenticateClientCallback: Failed to parse authentication message:
SyntaxError: Unexpected token < in JSON at position 0
at Object.parse (native)
at ClientManager.authenticateClientCallback (/var/www/node_modules/drupal-nodejs/lib/client-manager.js:196:21)
at Request._callback (/var/www/node_modules/drupal-nodejs/lib/client-manager.js:90:28)
at Request.self.callback (/var/www/node_modules/request/request.js:198:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request. (/var/www/node_modules/request/request.js:1035:10)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage. (/var/www/node_modules/request/request.js:962:12)
[2016/12/13 11:27:19] Body


401 Authorization Required

Authorization Required

This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.

I don't know what the problem is as there is a connection between the server and the host.

glekli’s picture

The nodejs/message path (e.g. http://yoursite.com/nodejs/message) needs to be accessible without logging in to Drupal. Certainly, if logintoboggan, or any other module, redirects from this path to the login page, that will prevent nodejs from working. Ensure that if you visit http://yoursite.com/nodejs/message in the browser, you get a json response.

SocialNicheGuru’s picture

I ended up disabling nodejs on user/login, user/register and any other page where there might be a redirect.