Yes you can use socket.io in your routes. You are getting the error "ReferenceError: io is not defined" because io is not defined lol. You need to include "var io = require('socket.io').listen(server). This will work perfectly in app.js, because in express apps, the server is defined as "vas server = createServer(app)" where you pass in the app. However in order to declare the server in the routes/index.js file, you need to create a "globalserver" that you can access from any file in your node project. To do this I reccommend you create a confg.json file, and create a config.js file.
Your config.json file should look something like this:
{
"all": {
"redis": {
"port": 6379
, "host": "127.0.0.1"
}
}
, "development": {
"env": "development"
, "db": "mongodb://...development"
}
, "staging": {
"env": "staging"
, "db": "mongodb://...staging"
, "redis": {
"pass": ""
}
}
, "production": {
"env": "production"
, "db": "mongodb://...production"
, "redis": {
"pass": ""
}
}
}
Your config.js file should look something like this:
var env = process.env.NODE_ENV || 'development'
, core = require('./config.json')
var merge = function (target, source) {
for (var k in source) {
if (typeof target[k] === 'object' && typeof source[k] === 'object')
merge(target[k], source[k])
else
target[k] = source[k]
}
}
module.exports = function (app) {
global.config = {}
merge(global.config, core.all)
merge(global.config, core[env])
global.config.app = {
key: app.key
, port: process.env.PORT || app.port
, base: app.base
}
merge(global.config, app[env])
}
Now in app.js
require('./config')({
key: 'pinpoint'
, port: 3000
, base: '/'
, development: {
app: {
url: 'localhost:3000'
}
}
, staging: {
app: {
url: 'localhost:3020'
}
}
})
var express = module.exports.express = require('express')
var bodyParser = require('body-parser');
var http = require('http');
var app = module.exports.app = express();
global.appserver = http.createServer(app);
var router = express.Router();
...
You have now successfully created a globalserver that you can access in your "routes/index.js"
Soooo Now in you index.js file you can do this:
global.io = require('socket.io').listen(global.appserver, { log:true});
io.sockets.on('connection', function(socket){
console.log("#### socket.io connected. Port 3000");
socket.on("scrape request", function(data) {
console.log("in here");
console.log(data);
})
})
You cannot do routes in the "on connection" function. However you can do a get/post call which calls a function outside of the get/post call which emits or recieves data. Hope this helps.