0

I'm trying to use the router and socket.io together.

I've created a separate router file and try to conjunct the route and socket.io

app = require('express.io')();
//var app = express();
//var router = express.Router();
app.http().io();



var mysql = require('mysql');
var connection = mysql.createConnection({
    host    :'aaaa',
    port : 3306,
    user : 'bbbb',
    password : 'cccc',
    database:'dddd'

});

connection.connect(function(err) {
    if (err) {
        console.error('mysql connection error');
        console.error(err);
        throw err;
    }
});



/* GET home page. */
app.get('/', function(req, res) {   
    var query = connection.query('select * from xe_livexe_rss limit 0,1',function(err,rows){
        console.log(rows);
        //res.json(rows);
        res.render('index', { title: 'Express',rss:rows });
        req.io.route('ready');      
    });

});

app.io.route('ready', function(req,res) {

    req.io.emit('talk', {
            message: 'io event from an io route on the server'
    });
}); 

module.exports = app;

but,,when I request the / router, it fails with the following message.

TypeError: Object #<Object> has no method 'emit'
    at Object.module.exports [as ready] (/home/ubuntu/nodetest1/routes/index.js:41:12)
    at Manager.io.route (/home/ubuntu/nodetest1/node_modules/express.io/compiled/index.js:85:29)
    at Object.request.io.route (/home/ubuntu/nodetest1/node_modules/express.io/compiled/index.js:197:29)
    at Query._callback (/home/ubuntu/nodetest1/routes/index.js:34:10)
    at Query.Sequence.end (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Sequence.js:78:24)
    at Query._handleFinalResultPacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
    at Query.EofPacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
    at Protocol._parsePacket (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Protocol.js:213:24)
    at Parser.write (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Parser.js:62:12)
    at Protocol.write (/home/ubuntu/nodetest1/node_modules/mysql/lib/protocol/Protocol.js:37:16)

Just wonder what I have missed...

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sooyong Kim
  • 221
  • 1
  • 3
  • 8

1 Answers1

0

By your error message, I can say callback fucntion for ready is not receiving a SocketRequest object and it gives you access to the RequestIO object which has an event your are trying to access emit(event, data).

I guess you code has a bug as higlited below:

/* GET home page. */
app.get('/', function(req, res) {   
    var query = connection.query('select * from xe_livexe_rss limit 0,1',function(err,rows){
        console.log(rows);
        //res.json(rows);
        res.render('index', { title: 'Express',rss:rows });
        req.io.route('ready');  // <== 
        //When this event is invoked there will be no req/res object passed to your `ready` event.
        //This has to be invoked by client script. like 
        //Emit ready event. 
         /* `io.emit('ready')`; */ <== clientPage.html
    });    
});

I have the following code working:

app = require('express.io')();
app.http().io();

// Setup the ready route, and emit talk event.
app.io.route('ready', function(req) {
    req.io.emit('talk', {
        message: 'io event from an io route on the server'
    })
});

// Send the client html.
app.get('/', function(req, res) {
    res.sendfile(__dirname + '/client.html')
});

app.listen(7076);
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164