Decrypter
Decrypter

Reputation: 3000

Using socket.io with routes in node.js

I have a problem using socket.io in routes.

In my app.js. I have specified my route.

app.get('/', routes.index);

I have an index.js file for my route

exports.index = function(req, res){
    res.render('index', { title: 'Example Title' });
    io.sockets.on('connection', function(socket){
    ...
    });
});

However, I keep getting the error "ReferenceError: io is not defined" in index.js. Do I need to pass the io object into each route or require socket.io in each route?

Upvotes: 0

Views: 2265

Answers (2)

user15163
user15163

Reputation: 641

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.

Upvotes: 0

Renato Gama
Renato Gama

Reputation: 16519

I will show you how I use socket.io in my app, even though I am not sure if this is the best practice in this matter.

In my app.js I have the following lines:

var io = require('socket.io').listen(server, { log: false });
routeRegistrar.init(app, io);

routeRegistrar is an auxiliary function that I use simply to go through every controller and register its routes, see:

var fs = require('fs');

var controllersFolder = "controllers";
var controllersFolderPath = __dirname + '/../' + controllersFolder + "/";

module.exports.init = function(app, io){
    fs.readdirSync(controllersFolderPath).forEach(function(controllerName){
        require(controllersFolderPath + controllerName).init(app, io);
    }); 
};

Note that I propagate the io var to every controller, so its available to every one! In the controller I have the following:

var sockets; //see that this variable becomes global to the controller
module.exports.init = function(app, io) {
    app.get("/chat", chat);

    sockets = io.sockets;
    sockets.on('connection', function(socket) {
        //do any cool stuff here
    });
};

function chat(){
    //sockets is available here, at the route level - so do more cool stuff here
}

Upvotes: 1

Related Questions