I'm trying to get socket.io working but now in Chrome I get the error:
Uncaught ReferenceError: require is not defined
client.php:9Uncaught ReferenceError: io is not defined
I changed the way to include the socket.io.js file because it dosnt exists somewher else:
<script src="/node_modules/socket.io/lib/socket.io.js"></script>
If I try
<script src="/socket.io/socket.io.js"></script>
I get: Failed to load resource: the server responded with a status of 404 (Not Found)
This is on Ubuntu with the latest of everything
I'm using the server code from http://socket.io/ to work with in the same folder like client.php and that works, named server.js, only modified port.
If your script isn't coming from your webserver, this will not work:
You have to explicitely state host and port:
Are you running node.js along PHP on your server?
There are two "socket.io" packages, a server and a client. You're trying to load the server one (
/node_modules/socket.io/lib/socket.io.js
) in the browser. The script you want is called socket.io-client and you can find it at https://raw.github.com/LearnBoost/socket.io-client/master/socket.io-client.jsWhat happens is that socket.io automatically serves the
/socket.io/socket.io.js
(the client one) file from port 80 when you're running node on port 80. In your case Apache is already at port 80, so you need to serve the file from it manually.Your library include (
/socket.io/socket.io.js
) in your client browser coding was probably OK. But you may be pointing to the wrong copy ofsocket.io.js
If you installedsocket.io
using (npm install socket.io
) then you might want to look in the following directory for the client version ofsocket.io.js
:C:\Program Files (x86)\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.js
If its there, then you may want to copy that module to your web publishing directory, OR as an alternative, you could change the physical path that your virtual directory points to.
Actually I was successful in getting Apache to serve up these files, today in fact.
Simply create a copy (I actually copied the files, soft links might be ok I did not try) of the contents of node_modules/socket.io/node_modules/socket.io-client/dist to your web root. For my install of socket.io (0.9.16) where I installed socket.io globally these two commands did the trick:
This was on a Ubuntu 13.10 box the usual LAMP stack installed before I then added node.js.
Now keeping this copy in sync as new versions of socket.io are released is a problem I have not yet tried to address yet.
Cheers
If you see something like
require() is not defined
, it may be because the JavaScript library you're using is in the format of an AMD (Asynchronous Module Definition). Try using RequireJS (full-featured) or the curl JS library (smaller, simpler) to load SocketIO instead.I had the same problem, and I confirm that @thejh's solution worked. I however was unsure as to "what" was the server when I read his recommendation. I do have MAMP running on port 80. And the code below would be the "node.js server", which runs on port 8001.
Once I started the node.js server (ran the code below), a visit to http://localhost:4001/socket.io/socket.io.js returns the javascript file.
For those within Drupal, a
drupal_add_js('http://localhost:4001/socket.io/socket.io.js', array('type' => 'external', 'group' => JS_LIBRARY));
within ahook_init
does the trick.