This question has been asked previously but haven't found any solution in prior replies.
Socket.IO gives me two problems:
- server side gave this error - ERROR - listen EACESS I read stackoverflow and resolved this with issuing a sudo command to start the server.
now clientside doesnt seem to find the socket.io.js file as per the script line -
I understand file is not found by using the chrome developer tools console which has a 404 error on the file.
I read that this file is created on the fly by server. But I did a 'ls-a' on the root folder. Couldn't find the socket.io/socket.io.js file.
Any ideas?
For reference here is my server code -
var http = require('http'),
path = require("path"),
url = require("url"),
fs = require("fs"),
mime = require("mime"),
io = require("socket.io").listen(server);
var homepath = ".";
var server = http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname;
var filepath = path.join(homepath, uri);
console.log(filepath);
path.exists(filepath, function (exists) {
if (!exists) {
//404 response
res.writeHead(404, {
"Content-Type": "text/plain"
});
res.write("404 File not Found \n");
res.end();
} else {
if (fs.statSync(filepath).isDirectory()) {
filepath += '/index.html';
filepath = path.normalize(filepath);
}
fs.readFile(filepath, "binary", function (err, data) {
if (err) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.write('500 File read error \n');
res.end();
} else {
var contentType = mime.lookup(filepath);
res.writeHead(200, {
'Content-Type': contentType
});
res.write(data, 'binary');
res.end();
}
});
}
});
//sockets part starts here
io.sockets.on('connection', function (socket) {
socket.on('test', function (data) {
console.log('i got something');
console.log(data.print);
});
});
});
server.listen(3000);
server.on('error', function (e) {
console.log(e);
});
console.log('Server listening on Port 3000');
The client file that you need is located inside the node_modules folder here:
Socket.io should serve this file though so you don't need to copy it somewhere else. For example if you are running your socket.io server on:
Then the script will be served from:
If you are consuming socket.io from another application or another port the relative URL in your code example won't work. You'll want to include the client-script manually or try including the client node module (if consumed by node app).
You can view the client repository here for more info: https://github.com/LearnBoost/socket.io-client
The problem here is that you're telling Socket.IO to listen on a server that does not yet exist, causing an
EACCES
and therefore not serving the client file. This is what you're doing:And if you saw in your server-side error console, you get this:
To fix this, move your listen function to after the server is created:
Once Socket.IO listens properly, it will automatically serve the client file to
/socket.io/socket.io.js
. You don't need to find it or serve it manually.