// set up ======================================================================
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 80;
var passport = require('passport');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var passportSocketIo = require('passport.socketio');
var RedisStore = require('connect-redis')(session);
var sessionStore = new RedisStore({ // Create a session Store
host: 'localhost',
port: 6379,
});
// optimization ================================================================
var compress = require('compression');
app.use(compress());
// configuration ===============================================================
require('./passport')(passport); // pass passport for configuration
// set up our express application
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs'); // set up ejs for templating
app.use( express.static( "public" ) );
// required for passport
app.use(session({
secret: 'sekret',
store: sessionStore,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, // 30 days
saveUninitialized: true,
resave: true
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ======================================================================
require('./routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
// socket.io====================================================================
//With Socket.io >= 1.0
io.use(passportSocketIo.authorize({
passport : passport,
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'express.sid', // the name of the cookie where express/connect stores its session_id
secret: 'sekret', // the session_secret to parse the cookie
store: sessionStore, // we NEED to use a sessionstore. no memorystore please
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail // *optional* callback on fail/error - read more below
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
accept(); //Let the user through
}
function onAuthorizeFail(data, message, error, accept){
if(error) accept(new Error(message));
console.log('failed connection to socket.io:', message);
accept(null, false);
}
io.on ('connection', function(socket) {
console.log("A USER LOGGED IN WITH ID: " + socket.request.user);
});
// launch ======================================================================
http.listen(port, function(){
console.log('\u001B[1m\u001B[42m\u001B[97m[SERVER] Started server on 127.0.0.1:'+port+'\u001B[0m');
});
client sidevar socket = io.connect('/');
I am missing something? Doing something wrong?
Exactly on this line "key: 'express.sid'", there's people who put 'express.key' and other that use 'express.sid' wich one is correct, or both of them are?
Hello, I've been searching for hours and I still can't solve this error.
I'm getting the "failed connection to socket.io: No session found" error.
Passport is working correctly and Redis stores sessions.
Redis
server.js
client side
var socket = io.connect('/');
I am missing something? Doing something wrong? Exactly on this line "key: 'express.sid'", there's people who put 'express.key' and other that use 'express.sid' wich one is correct, or both of them are?
Thanks!