Search code examples
node.jssessionsocket.ioexpress-session

Error - expressSessionMiddleware is not a function


I'm trying to use express-socket.io-sessions to sockets with the users' sessions, but I keep getting this error when I try add the middleware. Here is my code:

var express = require('express');
var socket = require('socket.io');
var sharedSession = require("express-socket.io-session");

function main(req, res, server, session) {

    res.sendFile(__dirname + "/views/find.html");

    var io = socket(server);

    io.use(sharedSession(session, {
        autoSave: true
    }));

}

All the parameters passed to the main function seemed when I console.log'd them, so I don't think it's that. Any help on why this error occurs would be much appreciated. Thanks.


Solution

  • You also have to middleware session into express app after that you can use the sharedSession middleware.

    Correct way

    var app = require('express')(),
      server  = require("http").createServer(app),
      io = require("socket.io")(server),
      session = require("express-session")({
        secret: "my-secret",
        resave: true,
        saveUninitialized: true
      }),
      sharedsession = require("express-socket.io-session");
    
    
    // Attach session
    app.use(session);
    
    // Share session with io sockets
    
    io.use(sharedsession(session));
    

    Reference to official documentation - https://www.npmjs.com/package/express-socket.io-session