const SocketIO = require('socket.io') let io const initIO = (httpServer) => { io = SocketIO(httpServer, { cors: { origin: 'http://localhost:3000' }, maxHttpBufferSize: 1e8 }) io.on("connection", socket => { console.log('CLIENT CONNECTED') socket.on("companySession", (companyId) => { console.log(`A client joined a companySession channel: ${companyId}`) socket.join(`company_${companyId}`) }); // socket.on("joinWhatsSession", (whatsappId: string) => { // logger.info(`A client joined a joinWhatsSession channel: ${whatsappId}`) // socket.join(`session_${whatsappId}`) // }) // socket.on("message_from_client", () => { // socket.emit("message_from_server", "Sent an event from the server!") // }) // socket.on("message_create", async (data: any) => { // handleMessage(data.msg, data) // }) // socket.on("media_uploaded", async (data: any) => { // handleMessage(data.msg, data) // }) // socket.on("message_ack", async (data: any) => { // handleMsgAck(data.id, data.ack) // }) // socket.on("joinChatBox", (ticketId: string) => { // logger.info("A client joined a ticket channel") // socket.join(ticketId) // }) // socket.on("joinNotification", () => { // logger.info("A client joined notification channel") // socket.join("notification") // }) // socket.on("joinTickets", (status: string) => { // logger.info(`A client joined to ${status} tickets channel.`) // socket.join(status) // }) socket.on("disconnect", (data) => { console.log(`Client disconnected socket: ${data}`) }) // socket.on("disconnecting", async () => { // console.log("socket.rooms: ", socket.rooms) // the Set contains at least the socket ID // let rooms = socket.rooms // console.log("rooms: ", rooms, " | rooms.size: ", rooms.size) // if (rooms && rooms.size == 1) return // if (rooms && rooms.size == 2 && ![...rooms][1].startsWith("session_")) // return // let whatsappIds: any = await Whatsapp.findAll({ // attributes: ["id"], // raw: true // }) // if (whatsappIds && whatsappIds.length > 0) { // whatsappIds = whatsappIds.map((e: any) => `${e.id}`) // console.log( // "whatsappIds whatsappIds whatsappIds whatsappIds whatsappIds: ", // whatsappIds // ) // if ( // rooms && // rooms.size == 2 && // [...rooms][1].startsWith("session_") && // whatsappIds.includes([...rooms][1].replace("session_", "")) // ) { // console.log([...rooms][1]) // let whatsappId = [...rooms][1].replace("session_", "") // const whatsapp = await Whatsapp.findByPk(whatsappId, {}) // if (whatsapp) { // await whatsapp.update({ status: "OPENING" }) // io.emit("whatsappSession", { // action: "update", // session: whatsapp // }) // } // } // } // }) }) return io } const getIO = () => { if (!io) { throw new AppError("Socket IO not initialized") } return io } module.exports = { initIO, getIO }