I came across this solution for sharing sessions between socket.io and express.js: Second answer (user xpepermint) in this post: socket.io and express 4 sessions
Unfortunately I am stuck with the bit of code below, as variable io
sits in config/socketio.js
and variable session
sits in config/express.js
:
io.use(function(socket, next) {
session(socket.handshake, {}, next);
});
Where do I put and how do I adapt the bit of code above?
My app.js file looks like the following:
'use strict';
import express from 'express';
import mongoose from 'mongoose';
mongoose.Promise = require('bluebird');
import config from './config/environment';
import http from 'http';
// Connect to MongoDB
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
});
// Populate databases with sample data
if (config.seedDB) { require('./config/seed'); }
// Setup server
var app = express();
var server = http.createServer(app);
var socketio = require('socket.io')(server, {
serveClient: config.env !== 'production',
path: '/socket.io-client'
});
global.clients = [];
global.socketio = socketio; //added
require('./config/socketio').default(socketio);
require('./config/express').default(app);
require('./routes').default(app);
// Start server
function startServer() {
app.angularFullstack = server.listen(config.port, config.ip, function() {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
}
setImmediate(startServer);
// Expose app
exports = module.exports = app;