Reputation: 758
i am new to node.js, I am trying to setup the Socket.io,but its giving 404 like this,
socket.io-1.4.7.js:1 GET http://localhost:9000/socket.io/?EIO=3&transport=polling&t=LeZf0Tg 404 (Not Found)Request.create @ socket.io-1.4.7.js:1Request @ socket.io-1.4.7.js:1XHR.request @ socket.io-1.4.7.js:1XHR.doPoll @ socket.io-1.4.7.js:1Polling.poll @ socket.io-1.4.7.js:1Polling.doOpen @ socket.io-1.4.7.js:1Transport.open @ socket.io-1.4.7.js:1Socket.open @ socket.io-1.4.7.js:1Socket @ socket.io-1.4.7.js:1Socket @ socket.io-1.4.7.js:1Manager.open.Manager.connect @ socket.io-1.4.7.js:2(anonymous function) @ socket.io-1.4.7.js:3
socket.io-1.4.7.js:1 GET http://localhost:9000/socket.io/?EIO=3&transport=polling&t=LeZf1h_ 404 (Not Found)
Here my Server side code is
var express = require("express");
var app = express();
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var config = require("./config/config");
var http = require('http').Server(app);
var io = require('socket.io')(http);
// configuration
mongoose.connect(config.database);
// template engine
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.set("views", __dirname + "/../client/views");
// use
app.use(express.static("../client/public"));
app.use(express.static("../client/app"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//app.use(morgan("dev"));
// Route handler for www requests
app.get('/*', function (req, res, next) {
if (req.headers.host.match(/^www/) !== null) {
res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
} else {
next();
}
});
app.use("/", require("./routes"));
app.use("/admin", require("./routes/admin"));
app.use("/test", require("./routes/testUser"));
var port = process.env.PORT || 9000;
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log("app listening on " + host + " " + port);
});
and my client side i am using Ejs templating engine, to create the Socket connection
<html>
<body>
<p class="dancing">Simple Blog</p>
<div class="social-icons">
<a href="https://twitter.com" target="_blank">
<i class="fa fa-twitter-square"></i>
</a>
<a href="https://github.com/" target="_blank">
<i class="fa fa-github-square"></i>
</a>
<a href="https://www.linkedin.com" target="_blank">
<i class="fa fa-linkedin-square"></i>
</a>
<a href="https://www.facebook.com" target="_blank">
<i class="fa fa-facebook-square"></i>
</a>
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io.connect('http://localhost:9000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</body>
</html>
can please anybody help me on this issue.
Upvotes: 0
Views: 716
Reputation: 2864
You have initialised socket.io with http require('socket.io')(http);
So you have to listen port like http.listen(port). Similar question answered here
Upvotes: 0
Reputation: 707258
You are not doing the socket.io initialization correctly on your server.
These two lines of code:
var http = require('http').Server(app);
var io = require('socket.io')(http);
Are creating a new http server and attaching socket.io to it and then that new server is never started.
Probably what you want to do is to share your same express server with socket.io (this is how socket.io is usually used).
In your code, you can do that by removing those two lines above and then inserting one line here at the end:
// code you already have
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log("app listening on " + host + " " + port);
});
// initialize socket.io to use your existing express server
var io = require('socket.io')(server);
Upvotes: 1