0

For a few days we'r trying to integrate drupal with node.js. but we couldn't connect with socket.io.js..

we're getting this error message from chrome console;

XMLHttpRequest cannot load http://mydomainname.com:8080/socket.io/1/?t=1340201859161. Origin http://mydomainname.com is not allowed by Access-Control-Allow-Origin.

and our backend settings are;

/**
* This configuration file was built using the 'Node.js server configuration builder'.
* For a more fully commented example see the file nodejs.config.js.example in the root of this module
*/
backendSettings = {
  "scheme":"http",
  "host":"mydomainname",
  "port":8080,
  "key":"/path/to/key/file",
  "cert":"/path/to/cert/file",
  "resource":"/sites/all/modules/nodejs/node_modules/socket.io/lib",
  "publishUrl":"publish",
  "serviceKey":"",
  "backend":{
  "port":80,
  "host":"urb5.com",
  "messagePath":"realtime"},
  "clientsCanWriteToChannels":false,
  "clientsCanWriteToClients":false,
  "extensions":["nodejs.server.extension.js"],
  "debug":true,
  "transports":["websocket",
  "flashsocket",
  "htmlfile",
  "xhr-polling",
  "jsonp-polling"],
  "jsMinification":true,
  "jsEtag":true,
  "logLevel":1};

and also, in source code we have a script socket.io script, like

<script type="text/javascript" src="http://mydomainname.com:8080/sites/all/modules/nodejs/node_modules/socket.io/lib/socket.io.js"></script>

this scripts build number is 0.9.6, but if we follow this path in ftp, there is a socket.io.js but its build number is 0.9.5

any suggestions?, thanks..

Bahadır
  • 269
  • 2
  • 3
  • 12

1 Answers1

0

The problem here, is that you are trying to load up socket.io from the server, but your front-end files are located in another domain space / server.
There is security regulations that does not allows cross-domain ajax and resources requests if they are not enabled by server.
So on server side where socket.io.js is coming from, you should add in page header something like this:

Access-Control-Allow-Origin: http://hello-world.example
Access-Control-Max-Age: 3628800
Access-Control-Allow-Methods: PUT, DELETE

This will allow you to share resource content with specified domain. And browser will not throw Access-Control-Allow-Origin error anymore.
As well, why you are trying to include js file through port 8080? If this is port that you bind your socket.io listener, then this is wrong, and you need to get js file through usual port (in most cases without defining, or 80).

moka
  • 22,846
  • 4
  • 51
  • 67
  • This also applies when the ports are different, such as in this case where one port is 80, and the other one is 8080. – apaderno Jun 21 '12 at 06:23
  • thanks for our answers, we'r using drupal7 on ubuntu and dont know which file we should add these codes. these front-end codes are generating by drupals nodejs module. also it didnt workd on port 80, server fired; "warn - error raised: Error: listen EADDRINUSE", there wasn't any server error while on 8080. – Bahadır Jun 21 '12 at 09:18
  • This error triggers when nodejs trying to run listening socket on already used port. – moka Jun 21 '12 at 15:54