title: How can i share socket.io into other modules in nodejs?
body: <p>I have the following structure of my project in node</p>
  
  <p><a href="https://i.stack.imgur.com/ZmRhK.png" rel="noreferrer">project structure</a></p>
  
  <p>in a app.js, let's say i have a app.js like this</p>
  
  <pre><code>'use strict'
  const express = require('express');
  
  const realtime = require('./controllers/realtime');
  
  const app = express();
  const server = require('http').Server(app);
  
  
  
  var sessionMiddleware = session(sessionConfig);
  realtime(server,sessionMiddleware);
  </code></pre>
  
  <p>in a controllers/realtime.js</p>
  
  <pre><code>"use strict";
  const config = require("../config");
  
  module.exports = (server,sessionMiddleware) =&gt; {
      const io = require('socket.io')(server);
  
      io.use(function(socket, next) {
          sessionMiddleware(socket.request, socket.request.res, next);
      });
  
      io.on('connection', (socket) =&gt; {
  
          socket.on('statusConnetion',(data)=&gt;{
              console.log(data)
          });
  
          socket.on('disconnect', function () {
              console.log(socket.id,"Un socket se desconecto");
          });
  
          console.log(`New socket connection: ${socket.id}`);
      });    
  }
  </code></pre>
  
  <p>in controllers/cargos.js</p>
  
  <pre><code>const express = require('express');
  const router = express.Router();
  let cargos = {};
  
  cargos.update = (req, res, next) =&gt;{
      //How can I use sockets here?
  }
  
  module.exports = cargos;
  </code></pre>
  
  <p>How can I use sockets in the file cargos.js and other controllers?</p>