title: Nodejs - Express - Socket.io How to send a message to a specific socket.id from an outside function?
body: <p>I'm having problem finding the right answer for this. I'm trying to send a message to a specific socket.id given I'm handling multiple users but I need to do it from another function which does not have access to socket.io.</p>
  
  <p>I need to send the message to the specific socket.id inside the function:</p>
  
  <pre><code>var authorizePublish = function(client, topic, payload, callback) {
  //here
  }
  </code></pre>
  
  <p>socketLib.js</p>
  
  <pre><code>/// Import Modules ///
  const mosca = require('mosca');
  const DeviceService = require('../services/device.service');
  const config = require('../config');
  const util = require('../modules/util.js');
  
  module.exports = async function(httpServer, sessionParser) {
      var io = require("socket.io")(httpServer); // For Sockets
  
      io.use(function(socket, next) {
          sessionParser(socket.request, socket.request.res, next);
      });
  
      io.sockets.on('connection', function(socket) {
          const userSessionId = socket.request.session.userId;
          const userId = socket.request.session.passport.user;
          const socketId = socket.request.session.id;
          if (userSessionId == '') 
              console.log('client connected');
          console.log(`Client connected: ${userSessionId}, with userid: ${userId}, with socket: ${socketId} conectado`);
  
          socket.on('msg:message', async function (data) {
              socket.emit('message', data);
          });          
  
          socket.on('disconnect', function(data) {
              console.log('user disconnected');
          });
      });
  
      /// Mosca  Settings ///
      var moscaServer = null;
      var moscaSettings = {  
          interfaces: [ { type: "mqtt", port: 1884 }, { type: "http", port: 5000, bundle: true, static: './' }
      ],};
      var debug = util.isDebug(),
          isAuth = util.isAuth()
  
      //// Mosca Server ////
      var dbHost = config.dbHost;
      moscaServer = new mosca.Server(moscaSettings); 
      moscaServer.on('ready', setup);
  
      var authenticate = function(client, username, callback) {
        console.log('-------- Authenticating MQTT user... --------');
  
        callback(null, flag);
  
        if(authenticate) client.user = username; 
      }
  
      /// Mosca Events ///
      moscaServer.on('published', function (packet, client) {
          var arr = packet.topic.split('/');
          if (arr.length !== 3) { return; }
      });
  
      /// Mosca Functions ///
      var authorizePublish = function(client, topic, payload, callback) {
        if (client.user == topic.split('/')[1]) {
  
          // socket.emit('message', payload.toString());  (Here is where I need to get access to the client socket.id in order to send him a message with the payload.
  
          callback(null, true);
        }
        else {
          callback(null, false);
        }
  
      }
  
      function setup() {
      if (isAuth) {
          moscaServer.authenticate = authenticate;
      }
      if (config.authPub === true) {
          moscaServer.authorizePublish = authorizePublish;
      }
      if(config.authSubs == true) {
          moscaServer.authorizeSubscribe = authorizeSubscribe;
      }
      console.log('Mosca server is up and running')
      }    
  
  }
  </code></pre>