I'm trying to get connect and socket.io to work together nicely and simply. I have the following code on server side:
var connect = require('connect'),
io = require('socket.io');
var app = connect().use(connect.logger('dev'));
var sio = io.listen(app);
app.listen(8000);
when i open http://localhost:8000/socket.io/socket.io.js i'm get error:
Cannot GET /socket.io/socket.io.js
And Socket.IO not work, i'm trying copy file and load from another location, but socket.io requests do not reach the server
SOLUTION
if anyone comes to this issue, you need to wrap the connect/express app in a node http.Server. The app.listen() method is a convenience method for this and returns the server:
var io = require('socket.io');
var app = connect();
var server = app.listen(3000);
io.listen(server);
or the following is equivalent:
var io = require('socket.io');
var http = require('http');
var app = connect();
var server = http.createServer(app);
server.listen(3000);
io.listen(server);
Related
I have discovered that there are at least 2 ways to go about doing this. The first way creates an HTTP server, although the 2nd way doesn't.
I am not able to find any concrete tutorial regarding this.
Case I
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000);
Case II
var app = require('express')();
var port = process.env.PORT || 3000;
var io = require('socket.io').listen(app.listen(port));
How are the two methods different? And why doesn't the second method require an HTTP server?
app.listen() creates the http server for you (a shortcut that express lets you use).
Here's the code for app.listen():
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
This Express code is here.
I want to create a websocket to add a communication between my angular app and my database. The app shall be able to save a question in a database & to notify the user when somebody answered.
Unfortunately I tried some tutorials which all don't work. I'm totally new to this because I use apache usually. So If you know a "working", very basic (beginner) tutorial, which doesn't require to install lots of additional things like yeoman etc. I would be glad.
In the current tutorial I get the error message:
You are trying to attach socket.io to an express request handler function. Please pass a http.Server instance.
My server.js:
var connect = require('connect'),
serveStatic = require('serve-static'),
socket = require('socket.io');
var server = connect();
server.use(serveStatic(__dirname+'/../client'));
server.listen(8080);
var io = socket.listen(server);
console.log("Server started and listen to http://127.0.0.1:8080");
without
var io = socket.listen(server);
it serves my static page.
This error is on my other approach:
has no method 'use'
My server.js
var connect = require('connect');
var http = require('http');
var serveStatic = require('serve-static');
var app = connect();
var server = http.createServer(app).use(serveStatic(__dirname+'/../client')).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);
As always if you struggle long the solution comes quick after posting. That one works for the beginning:
var connect = require('connect');
var http = require('http');
var app = connect();
var fs = require('fs');
var index = fs.readFileSync(__dirname+'/../client/index.html');
var server = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end(index);
}).listen(3000);
var socket = require('socket.io');
var io = socket.listen(server);
I've never understood how the below codes are equivalent:
Code 1:
var app = require("express")();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
...
io.use(...);
...
server.listen(3000, function(){});
Code 2:
var app = require("express")().listen(3000);
var io = require("socket.io")(app);
...
io.use(...);
Code 3:
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
...
io.use(...);
...
server.listen(3000, function(){});
Can you please help me understand what is happening here ? And is one approach preferable over the other (and under what circumstances) ? Thanks.
The snippets are not equivalent. Some are from older versions of express and socket.io and some are more recent. I would use a modified example from the socket.io documentation.
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
/* Setup Express */
app.get('/', function (req, res) {
...
});
/* Setup Socket.io */
io.on('connection', function (socket) {
...
});
server.listen(3000);
Express now just exposes a handler function app which you pass to an http server. Socket.io expects you to pass it an http server for it to plug into.
I need to access my socket.io instance in some differents files, how do you make it works?
Here is what i tried:
main.js
var app = express();
var sockets = require('./sockets');
sockets.listen(app)
sockets.js
var io = require('socket.io');
exports.listen = function(app) {
io = io.listen(app);
//...
}
exports.io = io;
SomeClass.js
var io = require('./sockets').io;
var SomeClass = function() {
var Clients = io.sockets.clients('room');
//io is undefined...
}
exports.SomeClass = SomeClass;
In your file, io.sockets is undefined because you are starting your server incorrectly. Socket.IO expects a HTTP server instance, and you are instead passing an Express instance. You are also trying to change a variable after it has been exported, and that does not work. This is what you should be doing:
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server):
server.listen(80);
If you still wanted to split this up into files, you would have to put your Socket.IO instance in your main application, and pass it to other modules when requiring them.
I recently updated socket.io to latest version which is 0.9.8
Last time I worked with the app, I did something like this:
io.sockets.emit("logged",this);
And it worked. Now I get:
TypeError: Cannot call method 'emit' of undefined
at new Agent (/var/www/panel/models/agent.js:29:16)
at module.exports (/var/www/panel/modules/app.js:35:47)
at Query.Client.query (/var/www/panel/node_modules/mysql/lib/client.js:108:11)
at Query.EventEmitter.emit (events.js:85:17)
at Query._handlePacket (/var/www/panel/node_modules/mysql/lib/query.js:51:14)
at Client._handlePacket (/var/www/panel/node_modules/mysql/lib/client.js:319:14)
at Parser.EventEmitter.emit (events.js:88:17)
at Parser.write.emitPacket (/var/www/panel/node_modules/mysql/lib/parser.js:71:14)
at Parser.write (/var/www/panel/node_modules/mysql/lib/parser.js:576:7)
at Socket.EventEmitter.emit (events.js:88:17)
I thought it was because something about scope or whatever so I just did this:
// server.js
var express = require('express'),
server = express(),
Routes = require('./routes'),
App = require('./modules/app.js'),
database = require('./libraries/mysql.js'),
io = require('socket.io'),
mysql = require('mysql');
server.listen(8080);
io.listen(server);
io.sockets.emit('Hi there', {});
And it just doesn't work, same error, different line.
What am I doing wrong?
You need to save the return value of socketio.listen and interact with that. I recommend you follow the examples at socket.io. Something like this should do the trick:
var express = require('express'),
server = express(),
socketio = require('socket.io');
server.listen(8080);
var io = socketio.listen(server);
io.sockets.emit('Hi there', {});
Or even:
var express = require('express'),
server = express(),
io = require('socket.io').listen(server);
server.listen(8080);
io.sockets.emit('Hi there', {});