node.js /socket.io/socket.io.js not found
Copying socket.io.js
to a public folder (something as resources/js/socket.io.js
) is not the proper way to do it.
If Socket.io
server listens properly to your HTTP
server, it will automatically serve the client file to via http://localhost:<port>/socket.io/socket.io.js
, you don't need to find it or copy in a publicly accessible folder as resources/js/socket.io.js
& serve it manually.
Code sample
Express 3.x -
Express 3 requires that you instantiate a http.Server
to attach socket.io
to first
var express = require('express')
, http = require('http');
//make sure you keep this order
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
//...
server.listen(8000);
Happy Coding :)
How to find socket.io.js for client side
install socket.io
npm install socket.io
find socket.io client
find ./ | grep client | grep socket.io.js
result:
./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
copy socket.io.js to your resources:
cp ./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js /home/proyects/example/resources/js/
in your html:
<script type="text/javascript" src="resources/js/socket.io.js"></script>
It seems that this question may have never been answered (although it may be too late for the OP, I'll answer it for anyone who comes across it in the future and needs to solve the problem).
Instead of doing npm install socket.io
you have to do npm install socket.io --save
so the socket.io module gets installed in your web development folder (run this command at the base location/where your index.html or index.php is). This installs socket.io to the area in which the command is run, not globally, and, in addition, it automatically corrects/updates your package.json file so node.js knows that it is there.
Then change your source path from '/socket.io/socket.io.js'
to 'http://' + location.hostname + ':3000/socket.io/socket.io.js'
.