How To Get Persistent Express Session Id In Connection?

I am building a MEAN stack chat application. And I want to be able to get express session id inside on connection handler. I am able to access the but it is not the same as Further keeps changing on every socket.

I have tried to solve this by getting session id from req.handshake.cookie by looking for connect.sid key but this key is not defined in there.

const cors = require('cors'),
express = require('express'),
eSession = require('express-session'),
cookieParser = require('cookie-parser'),
MongoStore = require('connect-mongo')(eSession),
sharedsession = require(''),
http = require('http'),
bodyParser = require('body-parser'),
socketio = require('');

const app = express();
const server = http.createServer(app);
const io = socketio(server);

const port = process.env.PORT || 3000;
const mongoose = require('./config/dbconnection');

const user = require('./api/routes/user');

const socketHandle = (io, mStore) => {
   io.on('connection', (socket) => {
     socket.on('join', (options, callback) => {

     console.log(socket.handshake.headers.cookie); //no connect.sid string present
     console.log( // has value but keeps changing on each io connection and this id is not the same as in an api endpoint method


const corsOptions = {
  origin: 'http://localhost:4200',
  optionsSuccessStatus: 200

const db = mongoose.connection;
const mStore = new MongoStore({ mongooseConnection: db });
const session = eSession({
  secret: 'my-secret',
  resave: true,
  saveUninitialized: true,
  store: mStore


socketHandle(io, mStore);

app.use(bodyParser.urlencoded({ extended: true }));


app.use('/user', user);

server.listen(port, () => console.log(`listening on http://localhost:${port}`));

Both and socket.handshake.headers.cookie(connect.sid key)should have provided the session id that has the same value as



There was not any issue with my code. The only thing that was causing different session id was the fact that I was running angular on a different port than node server. I had to ng build --watch for angular and output the files in node server's static files folder. By doing this, I was able to get consistent session id both in routes and socket io.