I am trying to develop a SCADA like app using Brian Ford's excellent https://github.com/btford/angular-socket-io-seed as a starting point, but I have run into some some JavaScript code that I just don't understand. Worse I don't even know what to search for. Every example I find via Google uses the second syntax which here at least does not work.
This code in the main app.js works, but I need access to the socket object so I can pass it to my under development simulation module, so I need to change it. But when I change the socket connect call back the module no longer gets loaded. I know when code in routes/socket.js gets run because the log line MET3: is from it.
Can somebody give me a clue what the original line is doing so I can make changes to it? Is this some cool new shorthand I should be using?
Not certain if it is relevant but I am running socket.io 0.9.16 and node.js 0.10.29.
var io = require('socket.io').listen(server);
// many lines latter
io.sockets.on('connection', require('./routes/socket.js')); // What is this?
Working Output
Express server listening on port 3000
debug - client authorized
info - handshake authorized hOEv8Iv7pPO1xLdxdq1V
MET1: routes/index.js index()
debug - setting request GET /socket.io/1/websocket/hOEv8Iv7pPO1xLdxdq1V
**MET3:** routes/socket.js Socket ID [hOEv8Iv7pPO1xLdxdq1V] connected
debug - websocket writing 5:::{"name":"send:name","args":[{"sockName":"JohnDoe","sockPage":"RETS"}]}
debug - websocket writing 5:::{"name":"send:time","args":[{"time":"Fri Jul 25 2014 17:39:32 GMT-0400 (EDT)"}]}
var io = require('socket.io').listen(server);
// many lines latter
io.sockets.on('connection', function (socket) {
console.log ("MET0: app.js io.sockets.on() running");
require('./routes/socket.js'); // This should work
});
Broken Output
Express server listening on port 3000
debug - client authorized
info - handshake authorized Hn9It34K2OCT6o8ceinF
**MET0:** app.js io.sockets.on() running
MET1: routes/index.js index()
debug - emitting heartbeat for client Hn9It34K2OCT6o8ceinF
debug - websocket writing 2::
debug - set heartbeat timeout for client Hn9It34K2OCT6o8ceinF
debug - got heartbeat packet
debug - cleared heartbeat timeout for client Hn9It34K2OCT6o8ceinF
io.sockets.on('connection', require('./routes/socket.js')); // What is this?
Essentially this says pass whatever is returned from require('./routes/socket.js') to the sockets function. If you aren't familiar with how requiring modules work in node, it is likely that /routes/socket.js contains something like the following:
module.exports = function() {
// Do some work
};
This means that the function above will be returned by the require call. Have a look inside socket.js and see what is returned.