0

I'm trying out socket.io in my MEAN stack application by following a tutorial, but I don't know how to continue. I have the code below in one of my node.js routes. The plan is to connect with the socket, but for this I need the server variable. Does anyone know how to import this into my route file?

Movie-chat route file in Node.js

var express = require('express');
var router = express.Router();
var socket = require('socket.io');

var io = socket(server);

io.on('connection', function(socket){
    console.log('made socket connection')
})

module.exports = router;

App.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var movieChatRoutes = require('./routes/movieChat');


var app = express();
mongoose.connect('mongodb://localhost:27017/moviemeter');


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
  next();
});

app.use('/movieChat', movieChatRoutes);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  res.render('index');
});

module.exports = app;

This is how they got it in the tutorial (by using app.listen())

enter image description here

tilly
  • 2,229
  • 9
  • 34
  • 64
  • Other relevant answers: [How can I export socket.io into other modules in nodejs](https://stackoverflow.com/questions/38511976/how-can-i-export-socket-io-into-other-modules-in-nodejs), [node socket.io across multiple files](https://stackoverflow.com/questions/48063925/node-socket-io-socket-on-across-multiple-files/48071510#48071510) and [Getting socket.io namespace from anywhere in the project](https://stackoverflow.com/questions/29829882/getting-socket-io-namespace-from-anywhere-in-the-project/29834332#29834332). – jfriend00 Feb 20 '18 at 16:25
  • and how do I get that server variable in the app.js file? I can't just use app.listen(3200) again right? (it does this in a seperate file, but I don't know how they are connected.) – tilly Feb 20 '18 at 16:46
  • either put the io init code in app.js so you already have it there, put it in its own module and export it or export it from where it is. – jfriend00 Feb 20 '18 at 16:59
  • I'm sorry if I misunderstand you, but I am talking about 'server' in the line 'var io = require('socket.io')(server);'. I am using a MEAN stack app so I don't know where to get this 'server' variable. – tilly Feb 20 '18 at 17:10
  • 1
    I guess i misunderstood. The answer is probably the same. You pass server into the router as those other answers show. Probably best to break io into its own module too and export io that you can then pass into routers that need it. – jfriend00 Feb 20 '18 at 17:15
  • Where is your `const server = app.listen()` statement? That's what gives you the `server` object so we can't really know how to share the `server` until we know where you have it in the first place. – jfriend00 Feb 21 '18 at 02:15

0 Answers0