2

This question has been asked previously but haven't found any solution in prior replies.

Socket.IO gives me two problems:

  1. server side gave this error - ERROR - listen EACESS I read stackoverflow and resolved this with issuing a sudo command to start the server.
  2. 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'); 
2
  • Did you install using npm? Commented Oct 14, 2013 at 14:03
  • yes i did install via npm. Commented Oct 14, 2013 at 14:39

2 Answers 2

5

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:

// the HTTP server doesn't exist yet var io = require('socket.io').listen(server); var server = http.createServer(); 

And if you saw in your server-side error console, you get this:

info: socket.io started warn: error raised: Error: listen EACCES 

To fix this, move your listen function to after the server is created:

var server = http.createServer(); var io = require('socket.io').listen(server); 

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.

Sign up to request clarification or add additional context in comments.

Comments

2

The client file that you need is located inside the node_modules folder here:

node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js 

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:

http://localhost:5000 

Then the script will be served from:

http://localhost:5000/socket.io/socket.io.js 

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

2 Comments

I am using node.js only. The port is 3000.However i have manually created a fileserver in nodejs without using express. So the server code would try to find a file located at socket.io/socket.io.js which it would not find. How would things work out then?
There's no file on disk at socket.io/socket.io.js. It's being served dynamically by socket.io from a different location (see the method here github.com/LearnBoost/socket.io/blob/master/lib/index.js#L145). If you're handling it yourself it's probably never getting to socket.io for this request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.