title: Javascript chat using socket.io : my code doesn't work while teacher's does (seem a lot the same)
body: <p>I'm following a JS course and I can't understand why my code doesn't work but the teacher's code does work. It seems like it's an &quot;order of lines&quot; thing but I'm not sure and can't figure this out.</p>
  <p>More specifically, the functionality that doesn't work is the chat (the socket part).</p>
  <p>Here's the teacher's code that works :</p>
  <pre><code>const express = require('express')
  const session = require('express-session')
  const MongoStore = require('connect-mongo')(session)
  const flash = require('connect-flash')
  const markdown = require('marked')
  const app = express()
  const sanitizeHTML = require('sanitize-html')
  
  let sessionOptions = session({
    secret: &quot;JavaScript is sooooooooo coool&quot;,
    store: new MongoStore({client: require('./db')}),
    resave: false,
    saveUninitialized: false,
    cookie: {maxAge: 1000 * 60 * 60 * 24, httpOnly: true}
  })
  
  app.use(sessionOptions)
  app.use(flash())
  
  app.use(function(req, res, next) {
    // make our markdown function available from within ejs templates
    res.locals.filterUserHTML = function(content) {
      return sanitizeHTML(markdown(content), {allowedTags: ['p', 'br', 'ul', 'ol', 'li', 'strong', 'bold', 'i', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], allowedAttributes: {}})
    }
    
    // make all error and success flash messages available from all templates
    res.locals.errors = req.flash(&quot;errors&quot;)
    res.locals.success = req.flash(&quot;success&quot;)
  
    // make current user id available on the req object
    if (req.session.user) {req.visitorId = req.session.user._id} else {req.visitorId = 0}
    
    // make user session data available from within view templates
    res.locals.user = req.session.user
    next()
  })
  
  const router = require('./router')
  
  app.use(express.urlencoded({extended: false}))
  app.use(express.json())
  
  app.use(express.static('public'))
  app.set('views', 'views')
  app.set('view engine', 'ejs')
  
  app.use('/', router)
  
  const server = require('http').createServer(app)
  const io = require('socket.io')(server)
  
  io.use(function(socket, next) {
    sessionOptions(socket.request, socket.request.res, next)
  })
  
  io.on('connection', function(socket) {
    if (socket.request.session.user) {
      let user = socket.request.session.user
  
      socket.emit('welcome', {username: user.username, avatar: user.avatar})
  
      socket.on('chatMessageFromBrowser', function(data) {
        socket.broadcast.emit('chatMessageFromServer', {message: sanitizeHTML(data.message, {allowedTags: [], allowedAttributes: {}}), username: user.username, avatar: user.avatar})
      })
    }
  })
  
  module.exports = server
  </code></pre>
  <p>And here's my code, that seems a lot the same but doesn't work :</p>
  <pre><code>const express = require('express')
  const session = require('express-session')
  const MongoStore = require('connect-mongo')(session)
  const flash = require('connect-flash')
  const markdown = require('marked')
  const sanitizeHTML = require('sanitize-html')
  const app = express()
  
  let sessionOptions = session({
      secret: &quot;Javacsript is soooooooo coooooool&quot;,
      store: new MongoStore({client: require('./db')}),
      resave: false,
      saveUninitialized: false,
      cookie: {
          maxAge: 1000 * 60 * 60 * 24,
          httpOnly: true
      }
  })
  
  const router = require('./router')
  
  app.use(sessionOptions)
  app.use(flash())
  app.use(function(req, res, next) {
      // Make our markdown function available from within ejs templates
      res.locals.filterUserHTML = function(content) {
          return sanitizeHTML(markdown(content), {allowedTags: ['p', 'br', 'ul', 'ol', 'li', 'bold', 'i', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], allowedAttributes: {}})
      }
      // Make all error and success flash messages available from all templates
      res.locals.errors = req.flash(&quot;errors&quot;)
      res.locals.success = req.flash(&quot;success&quot;)
      // Make user session data available from within view template
      res.locals.user = req.session.user
      // Make current user id available on the req object
      if (req.session.user) {req.visitorId = req.session.user._id} else {req.visitorId = 0}
      next()
  })
  // Add user submitted data to route
  app.use(express.urlencoded({extended: false}))
  app.use(express.json())
  
  app.use('/', router)
  app.use(express.static('public'))
  // For views (1st arg), look into the folder 'views' (2nd arg)
  app.set('views', 'views')
  app.set('view engine', 'ejs')
  
  const server = require('http').createServer(app)
  const io = require('socket.io')(server)
  
  io.use(function(socket, next) {
      sessionOptions(socket.request, socket.request.res, next)
  })
  
  io.on('connection', function(socket) {
      if (socket.request.session.user) {
          let user = socket.request.session.user
  
          socket.emit('welcome', {username: user.username, avatar: user.avatar})
  
          socket.on('chatMessageFromServer', function(data) {
              // Emit the event to all connected users except the one who sent it
              socket.broadcast.emit('chatMessageFromServer', {
                  message: sanitizeHTML(data.message, {allowedTags: [], allowedAttributes: {}}), 
                  username: user.username, 
                  avatar: user.avatar})
          })
      }
  })
  
  module.exports = server
  </code></pre>
  <p>Thanks in advance for your help.</p>