'export a function inside socket connection

I have created a socket server as shown below.

const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server, {cors:{origin:'*'}})
const mongoose= require("mongoose")


const port = process.env.PORT || 4002;

server.listen(port, ()=>{
    console.log(`Listening on port ${port}......`)
})

onlineUsers = [];

const addNewUser = (userId, socketId)=>{
 !onlineUsers.some((user)=>user.userId === userId) && 
  onlineUsers.push({userId,socketId})
}

const removeUser= (socketId) =>{
    onlineUsers = onlineUsers.filter((user)=> user.socketId!==socketId)
}

const getUser = (userId)=>{
return onlineUsers.find(user=>user.userId ===userId)
}


io.on("connection", (socket=> {
console.log("User connected:",  socket.id);

socket.on("disconnect",()=>{
    removeUser(socket.id)  
})

socket.on("newUser",(userId)=>{
addNewUser(userId, socket.id)  
})


export function handleMessaging (userId,clientID,messageId )
{
const receiver = getUser(userId);

if(receiver)
{
io.to(receiver.socketId).emit("sendMessage", {data:"working properly"});
return true;
}
else return false
}

}));

I want to export the function handle messaging so that I can use it inside the API like (shown below) to see if a user is online and if yes, send a message.

But as someone new to programming, I can't figure out how to export handle messaging the proper way. I tried to use export but its telling me "Modifiers cannot appear here".


router.post('/:companyId' async (req, res) => {

const {userId,clientId,messageId} = req.body

handleMessaging (userId,clientID,messageId )
{
//do xyz

}


}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source