title: Socket.io emit event from express controller
body: <p>I need to emit the socket from the server from controllers, but I'm confuse in how to pass the io instance.
  Here's a example of my code to show you the structure of my project:</p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>'use strict'
  
  const mongoose = require('mongoose')
  const app = require('./app')
  const server = require('http').Server(app)
  const io = require('socket.io')(server)
  const config = require('./config')
  
  mongoose.connect(config.db, {useMongoClient: true}, (err, res) =&gt; {
    if (err) return console.log(`Error: ${err}`)
    console.log('Success')
  })
  
  io.on('connection', (socket) =&gt; {
    console.log(`Socket id: ${socket.id}`)
  
    socket.emit('welcome', { message: 'Welcome to API REST' }
  })
  
  server.listen(config.port, () =&gt; {
    console.log(`Running on port ${config.port}`)
  })
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>'use strict'
  
  const express = require('express')
  const bodyParser = require('body-parser')
  const path = require('path')
  const hbs = require('express-handlebars')
  const app = express()
  const api = require('./routes/api')
  const web = require('./routes/web')
  
  app.use(bodyParser.urlencoded({ extended: false }))
  app.use(bodyParser.json())
  
  app.engine('.hbs', hbs({
    defaultLayout: 'default',
    extname: '.hbs'
  }))
  app.set('view engine', '.hbs')
  
  app.use(express.static('public'))
  app.use('/scripts', express.static(path.join(__dirname, 
  '/node_modules/')))
  
  app.use('/api', api)
  app.use('/', web)
  
  module.exports = app
  </code></pre>
  
  <p><strong>routes/api.js</strong></p>
  
  <pre><code>'use strict'
  
  const express = require('express')
  const userController = require('../controllers/user')
  const api = express.Router()
  
  api.get('/users', userController.getUsers)
  
  module.exports = api
  </code></pre>
  
  <p><strong>controllers/user.js</strong></p>
  
  <pre><code>'use strict'
  
  const User = require('../models/user')
  
  function getUsers (req, res) {
     User.find({}, '-__v -password', (err, users) =&gt; {
      if (err) return res.status(500).send({ message: err.message, code: 
   err.code })
  
      res.status(200).send(users)
    })
  }
  </code></pre>
  
  <p>For example, I need to use the io instance in controllers/user.js</p>
  
  <p>Whats the best solution for this?</p>
  
  <p>Thanks!</p>