Can't find socket.io.js - node.js

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');
});

Related

socket.io is not found with Express 4?

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>

nodejs socket.io express handshake error

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.

socket.io not working node.js

I am not able to run socket.io code in node.js, console.log() is also not displaying when running the code. Below is the code.
app.js
var express = require('express');
var http = require('http');
var app = express();
app.set('port', process.env.PORT || 3000);
app.post('/testStream',test.testStream);
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
module.exports.appServer = server;
and I have created a test.js file where I am accessing this exported variable appServer.
var server = require('../app.js');
exports.testStream = function(req,res){
var io = require('socket.io').listen(server.appServer);
io.on('connection',function(socket){
console.log("in socket");
fs.readFile('E:/temp/testimg.png',function(err,buf){
socket.emit('image',{image: true,buffer: buf});
console.log("test image");
});
})
}
when the code runs it stucks and not showing the console.logs(). What I am doing wrong over here. Any help is very much appreciated.
I would suggest following the code structure as suggested in socket.io docs.
Also, you should not be calling io.listen or io.on('connection') inside your testStream express middleware. These are things you should only be doing once, and ideally they should happen during startup, inside app.js and not in reaction to a POST request. In fact, I'm not sure what the purpose of your testStream middleware is, its not even returning any response (eg res.end())
If you want to handle socket connections in a separate module you can, but instead of exporting your app's server the way you are, try passing the io instance as variable to your submodule. In short, try this:
app.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var test = require('./test')(io);
app.set('port', process.env.PORT || 3000);
server.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
test.js
module.exports = function(io) {
io.on('connection', function(socket) {
console.log("in socket");
fs.readFile('E:/temp/testimg.png', function(err, buf) {
socket.emit('image', {
image: true,
buffer: buf
});
console.log("test image");
});
});
};

Socket.io polling 404 in Laravel

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
});

How to connect Two Socket.io Node Application using Socket.io-client in Node js

In my application i need to connect two socket.io node applications.Using socket.io-client we can do like this.But i don't know how socket.io-client works and where to include that.
First Node Application
var express = require('express')
, http = require('http');
var app = express();
app.use(function (req, res) {
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
io.sockets.on('connection',function(socket){
socket.on('eventFiredInClient',function(data){
socket.emit('secondNodeAppln',data);// i need to get this event in my 2nd node application how can i do this by using socket.io-client
});
});
Second Node Application
var express=require('express');
var http=require('http');
var app=express();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var serverAddress = '127.0.0.1';
var serverPort = 3000; //first node appln port
var clientio = require('socket.io-client');
var socket = clientio.connect(serverAddress , { port: serverPort });
socket.on('connect', function(){
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
var io = require('socket.io').listen(server);
server.listen(6509);
//here i need to get the 'secondNodeAppln' event raised in first node application.How can i do this.
You need to create a socket.io client in your first app:
var io = require('socket.io').listen(server); // this is the socket.io server
var clientio = require('socket.io-client'); // this is the socket.io client
var client = clientio.connect(...); // connect to second app
io.sockets.on('connection',function(socket) {
socket.on('eventFiredInClient',function(data) {
client.emit('secondNodeAppln', data); // send it to your second app
});
});
And in your second app, just listen for those events:
io.sockets.on('connection', function (socket) {
socket.on('secondNodeAppln', function(data) {
...
});
});
There's a bit of a race condition because the code above doesn't wait for a connect event on the client socket before passing events to it.
EDIT see this gist for a standalone demo. Save the three files to a directory, start the servers:
node serverserver &
node clientserver
And open http://localhost:3012 in your browser.

Resources