Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 798
Johan Svensson Avatar asked Nov 20 '11 14:11

Johan Svensson


2 Answers

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>
like image 160
thejh Avatar answered Sep 17 '22 04:09

thejh


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.

like image 26
Ricardo Tomasi Avatar answered Sep 20 '22 04:09

Ricardo Tomasi