Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve a Socket.io 404 (Not Found) error?

I am using Socket.io with Express. In my project I have a login page and a home page. When I do successful login, I navigate to localhost:3000/home where I get this error:

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1418187395022-0 404 (Not Found) 

I did not do any modification in my app.js (project created by express ).

index.js:

var express = require('express'); var router = express.Router(); var http = require('http'); var fs = require('fs'); var io = require('socket.io')(http); /* GET home page. */ router.get('/', function(req, res) {   res.render('index', { title: 'Express' }); });  router.get('/home', function(req, res) {   res.render('home', { title: 'Express' }); });  io.on('connection', function(socket){     console.log("User Connected");   socket.on('chat message', function(msg){     io.emit('chat message', msg);     console.log("Message");   });    socket.on('disconnect', function(msg){     console.log("User DisConnected");   });  });  router.post('/authenticate', function(req, res) {     fs.readFile("./public/Verification/Users.json", "utf8", function (err, data) {         if (err)              console.log(err);         else{             var result = JSON.parse(data);             for(var i=0;i<result.Users.length;i++){                 if(req.body.username == result.Users[i].username && req.body.password ==     result.Users[i].password){                     console.log("Success!!!!!!!!!!!!!!");                     res.location("home");                     res.redirect("home");                 }             }         }         }); });  module.exports = router; 

In my layout.jade I defined Socket.io like this:

script(src='https://cdn.socket.io/socket.io-1.2.0.js') 
like image 650
Zee Avatar asked Dec 10 '14 05:12

Zee


People also ask

Why is Socket.IO not working?

First and foremost, please note that disconnections are common and expected, even on a stable Internet connection: anything between the user and the Socket.IO server may encounter a temporary failure or be restarted.

Why is Socket not connected?

The error message “ERR_SOCKET_NOT_CONNECTED” is instantly solved in the majority of the cases when we flush the sockets on your browser. This will break the connection between any active pages on your browser and you might have to reinitialize everything.

How do I test a Socket.IO connection?

You can check the socket. connected property: var socket = io. connect(); console.

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!


1 Answers

If you're running Express 4, it appears to me like you're missing the lines of code:

var app = express(); var server = app.listen(3000); 

That will start your web server and set it to port 3000. See the bone simple Express 4 app here: http://expressjs.com/4x/api.html

And, then to start up socket.io, you would add:

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

And, there is no need for this line:

var http = require('http'); 
like image 84
jfriend00 Avatar answered Oct 24 '22 16:10

jfriend00