Can't emit by socket.io? - node.js

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

Related

Websocket With Socket.io & Angular

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

Cannot call method "on" of undefined (express and socket.io)

I want to export the socket.io object from my app.js of express
My app.js file is here: http://pastebin.com/Kfny4yVK
My bin/www file is here: http://pastebin.com/qGhPm6KE
In my app.js I do:
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
exports.io = io;
In a routes file I do:
var io = require(__dirname+'/../app.js').io;
However, when I call the:
io.on
I get an undefined object error. Any idea why this is happening? It looks like nodejs is somehow modifying the io object at export? Is this possible? Is there a way to make this work?
With Express v4.x I use the following to export the socket.io object.
APP.JS
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//Export sio module so socket.io can be used in other modules
module.exports.sio = io; //ADDED this
OTHER_FILE.JS
//Import sio module from app.js
var io = require('../app.js').sio;

express and socket.io - declaration and starting 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.

how to use Express 4 with Socket.io 0.9.x?

I am a ios developer, the Objc-Socket.io library is still support socket.io 0.9.x
I use Express 4.0 with my nodejs server:
server.js
var express = require('express')();
var io = require('socket.io');
io.listen(express);
But I run it in terminal, it throw me an error:
% node test
Socket.IO's `listen()` method expects an `http.Server` instance
as its first parameter. Are you migrating from Express 2.x to 3.x?
If so, check out the "Socket.IO compatibility" section at:
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
This code should do the trick:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app).listen(1337);
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
//emit and listen messages
});
app.get('/', function(req, res) {
//do something
});

Connect2 and Socket.io

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

Resources