I have been trying to get my client connected with my server,but chrome always print that
GET http://localhost:30653/socket.io/socket.io.js 404 (Not Found)
I don't know where's the problem...
the express version is "4.13.4" and socket.io version is "1.4.5"
And here is my code:
app.js
var express = require('express');
var hbs = require('hbs');
var app=express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.on('connection',function(socket){
console.log("connected");
socket.emit('open');
});
app.set('port', process.env.PORT || 30653);
app.set('view engine','html');
app.engine('html',hbs.__express);
app.use(express.static('public'));
app.get('/',function(req,res){
res.render('chatroom');
});
app.listen(app.get('port'),function(){
console.log('this server is listening on port:'+app.get('port'));
});
client:
$(function(){
var socket = io.connect('http://localhost:30653');
socket.on('open',function(){
console.log("open")
});
socket.on('system',function(json){
console.log("system");
});
});
any help is welcome!I'll be very appreciate it!
I think your app.listen(...) needs to be server.listen(...) because of the way you are creating your server as illustrated here: http://socket.io/docs/#using-with-express-3/4. The way you are doing it, socket.io is not hooked to the right server and thus is not serving the socket.io.js file for you.
You can do app.listen(), but only if you follow a different initialization procedure here: http://socket.io/docs/#using-with-the-express-framework
You need use the below line of code in app.js after the line app.use(express.static('public'));
app.use("/lib", express.static(path.join(__dirname,'node_modules')));
and then import in your client as below
<script src='/lib/socket.io/socket.io.js' type='text/javascript'></script>
Related
I try to create a real time app by socket.io.
Server side:
var express = require('express');
var io = require('socket.io');
var engine = require('ejs-locals');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.redirect('/login')
});
app.use(express.static(__dirname + '/public'));
app.listen(3001);
io.sockets.on('connection', function (socket) {
console.log('Client connected...');
socket.on('send_login_data', function (data) {
console.log(data);
});
});
Client side:
var socket = io.connect('http://localhost:3001');
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
socket.on('connecting', function () {
console.log('connecting...');
});
socket.on('connect', function () {
console.log('connected!');
});
I caught the next error:
GET http://localhost:3001/socket.io/1/?t=1447539302809 404 (Not Found)
As I understand, it's a handshake error.
How can I fix it?
Thanx.
First off, make absolutely sure there are no errors showing anywhere in case some module isn't installed properly.
Then, make sure you have the same version number of socket.io on client and server and that you have the server-side version installed on the server.
Then, I've seen folks have issue with this before, even when following the instructions on the socket.io web site and it's never been clear exactly what was wrong with that sequence. But, what I know is that this sequence will work:
var express = require('express');
var app = express();
var server = app.listen(3001);
var io = require('socket.io').listen(server);
See related issues: Where is my client side socket.io? and Node + Socket.io Connection issue
I don't know if this is causing the issue or not, but you are attempting to redefine the io variable when it has already been declared in this:
var io = require('socket.io');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
The second reference to io = io.listen(server) is essentially this:
var io = require('socket.io');
var app = express();
var server = require('http').createServer(app);
var io = io.listen(server);
Which is not correct Javascript. Again, may not be causing your issue, but it is not technically proper Javascript.
I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.
I am trying to implement a chat app with Socket.io
in to my Laravel app. The chat app works fine on it's own,
but I am having problems to make it work in Laravel.
I try to serve Laravel on port 8000 and the chat server on 8000.
I use Express 4.8.0 and Socket.io 1.0.6, Node 0.10.29 and nodemon for testing.
//server.js:
var express = require('express');
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
http.listen(8000, function () {
console.log('listening on *:8000');
});
app.use('/', express.static(__dirname + '/public'));
app.get("/*", function (req, res){
res.sendFile(__dirname + "/index.php");
});
//client.js:
var socket = io.connect('http://localhost:8000');
//html - dependencies, I tried all these:
<script src="//cdn.socket.io/socket.io-1.0.0.js"></script>
{{ HTML::script('/socket.io/socket.io.js') }}
<script src="http://localhost:8000/socket.io/socket.io.js" ></script>
<script src="{{asset('/socket.io/socket.io.js')}}"></script>
and then for the client side (own code)
{{ HTML::script('js/client.js') }}
The CDN version of Socket.io gives constantly these kinds of logs:
"GET http://localhost:8000/socket.io/?EIO=2&transport=polling&t=1407425555977-15 404 (Not Found)".
The others ones just gives a js file not found log:
"GET http://localhost:8000/socket.io/socket.io.js 404 (Not Found)"
//folder structure:
/public
/js
client.js
/node_modules
server.js
Can anyone see what I can do to make it work?
EDIT
//server.js
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
io.on('connection', function (socket) {
console.log("Connected server");
}
server.listen(8000);
//client.js
var socket;
$(document).ready(function () {
socket = io.connect('http://localhost:8000');
});
//When I typ the global "socket" object in the log it says:
connected: false
disconnected: true
This is because you have set it up incorrectly. I had the same exact problem you did (same errors and basic code layout). You need to do npm install socket.io --save while in the base directory of your page (the same as where your index.php file is located). Then you have to do the same for express (npm install express --save). You also have to change your server code. Change the creation of io from:
var express = require('express');
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
To:
var socket = require('socket.io');
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = socket.listen(server);
Then remove the app.use and app.get as they are no longer needed for how this is going to be done. Then add server.listen(8000); at the end of the server.js. For dependencies, use: <script src="//cdn.socket.io/socket.io-1.0.0.js"></script>. Then, to run your server, go to it in terminal and type node server.js. Then just connect to it with your client. Also, for events, in the server, use:
io.on('connection', function (client) {
client.on('someEvent', function(someVariables){
//Do something with someVariables when the client emits 'someEvent'
io.emit('anEventToClients', someData);
});
client.on('anotherEvent', function(someMoreVariables){
//Do more things with someMoreVariables when the client emits 'anotherEvent'
io.emit('anotherEventToClients', someMoreData);
});
});
And in your client code:
socket.emit('someEvent', variables);
socket.on('anEventToClients', function(something){
//Code when anEventToClient is emitted from the server
});
I am struggling with an Express/Node/PeerJs application that uses socket.io. It works in localhost but not when pushing to heroku or nodejitsu.
Here is what I have in app.js:
var routes = require('./routes');
var express = require('express');
var path = require('path');
var https = require('https');
var fs = require('fs');
var url = require('url');
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io');
app.engine('.html', require('ejs').__express);
app.set('port', process.env.PORT || 5500);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
...
console.log(process.env.port);
app.configure('development', function(){
app.use(express.errorHandler());
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var sockets = io.listen(server);
app.get('/', function(req, res){
res.render('index');
});
//more code
In chatroom.html:
<script type="text/javascript"></script>
<script src="myapp.herokuapp.com:5500/socket.io/socket.io.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
var socket = io.connect('http://myapp.herokuapp.com:5500/');
var peer = new Peer({key: 'mykey', debug: true});
peer.on('open', function(peer) {
peer_id = peer;
console.log('my peer id is ' + peer_id);
socket.emit('peer', {peer_id: peer_id, chatroom: chatroomString});
console.log('peer id and chatroom string: ', {peer_id: peer_id, chatroom: chatroomString});
});
//rest of code
I've tried changing the path to /socket.io/socket.io.js, adding http, changing ports, and many other silly things. Yet unfortunately it works in localhost but not when I push to both nodejitsu and heroku.
There are some relatively similar questions(that I could find) but none with sufficient answers.
Socket.io Failed to load resource
socket.io: Failed to load resource
Would appreciate the help.
SOLUTION!!!
in my app.js file
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var sockets = io;
in my chatroom.html file
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://myapp.herokuapp.com/');
You just take the port numbers out of your html file.
Websockets was not supported earlier in Heroku. Now it is supported. You need to set flag for that.
https://devcenter.heroku.com/articles/heroku-labs-websockets
Even after setting the flag, and restarting the app, the sockets are not working, please clarify the exact error that you are getting.
I'm making a web-app that makes use of nodejs, mongodb, socket.io, express and mongoose.
I can start my server and correctly get the wanted html file in my browser when browsing to my localhost.
The problem I have is getting my socket.io to work.
On my server side everything works fine : I get " info - socket.io started " in my terminal.
But when surfing to my browser I get this in my browser console
Failed to load resource: the server responded with a status of 404 (Not Found)
Uncaught ReferenceError: io is not defined
This is how i connect to socket.io.js
<script src="/socket.io/socket.io.js"></script>
and my map structure looks like this:
map
-app.js
-public
--index.html
-node_modules
--socket.io
--mongodb
--express
--jade
--mongoose
Does anyone knows what the mistake is I've made?
(it's actually the same problem as here: Node.js socket.io.js not found or io not defined )
Thanks in advance!
EDIT:
My code on the server side is this:
var app= require('express').createServer();
var io = require('socket.io').listen(app);
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/db');
app.listen(3030);
console.log("server started");
app.get('/',function(req,res){
res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection',function(socket){
console.log("connection made");
});
The first log, gets logged in the terminal ("server started"), but the second one ("connection made") doesn't get logged. So the connection isn't made.
I thought that was because of the wrong "set up" in my client side.
Check out the express migration guide 2->3
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
Something like this should work
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/db');
server.listen(3030);
console.log("server started");
app.get('/',function(req,res){
res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection',function(socket){
console.log("connection made");
});
var app = express();
app.set('port', process.env.PORT || 3000);
...
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a hot socket');
});