Johan Svensson
Johan Svensson

Reputation: 872

Socket.IO - require is not defined

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.

Upvotes: 18

Views: 31217

Answers (8)

James Powell
James Powell

Reputation: 813

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:

sudo cp -av /usr/lib/node_modules/socket.io/node_modules/socket.io-client/dist /var/www/socket.io 
chown -R www-data:www-data /var/www/socket.io

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

Upvotes: 0

Ricardo Tomasi
Ricardo Tomasi

Reputation: 35253

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.js

What 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.

Upvotes: 16

JosephK
JosephK

Reputation: 678

As others have noted, when io is run on the server side, an "automagical" service of "socket.io.js" is served - and it branches out to the other js files in its lib directory.

What I wish to add, is that This is Not Intutive and, no, you cannot have your regular webserver serve this file - the errors just get more strange and seemingly contradictory (require not defined, etc). This is not a normal js file, and I wish all the tutorials out there would emphasize that.

Anyhow, if you are running behind nginx, for example, set the port-settings to the same port assigned for node - port 3000 in my case. I used that port in the client like this:

  <script src="http://localhost:3000/socket.io/socket.io.js"></script>

... and then also used port 3000 in the server code - there are too many ways / versions you can code the server-js-code for an example to do anything but confuse most who are using 'a different way'.

Upvotes: 1

Bob
Bob

Reputation: 51

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 of socket.io.js If you installed socket.io using (npm install socket.io) then you might want to look in the following directory for the client version of socket.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.

Upvotes: 5

Scott Grodberg
Scott Grodberg

Reputation: 166

I tried what thejh says and it works. I needed to specify the host and port. For a node app listening on port 81, I needed to specify port 81 in the client javascript. Nevermind /socket.io/socket.io.js doesn't exist in the vhost, node makes up for it at runtime.

Upvotes: 1

amateur barista
amateur barista

Reputation: 4520

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.

/* Node.js server */

var sys  = require('sys');
var io   = require('socket.io');
var http = require('http')
server = http.createServer(function(req, res) { 
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('http server started');
  res.end(); 
});
server.listen(4001);


// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client) {

  client.on('message', function(message) {
    // We're in!
    console.log('received client message '+ message);
  }); 
  client.on('disconnect', function() {});
  socket.send('welcome to the local node.js server!');
});

For those within Drupal, a drupal_add_js('http://localhost:4001/socket.io/socket.io.js', array('type' => 'external', 'group' => JS_LIBRARY)); within a hook_init does the trick.

Upvotes: 0

a paid nerd
a paid nerd

Reputation: 31502

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.

Upvotes: 0

thejh
thejh

Reputation: 45568

If your script isn't coming from your webserver, this will not work:

<script src="/socket.io/socket.io.js"></script>

You have to explicitely state host and port:

<script src="http://localhost:<port>/socket.io/socket.io.js"></script>

Upvotes: 33

Related Questions