Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in send Message Route TypeError: getReceiverSocketId is not a function #16

Open
mannaayan opened this issue May 13, 2024 · 0 comments

Comments

@mannaayan
Copy link

Error in send Message Route TypeError: getReceiverSocketId is not a function

const Conversation = require("../models/conversation.models");
const Message = require("../models/message.models");
const { io } = require("../app");
const getReceiverSocketId = require("../app").getReceiverSocketId;

const sendMessage = async(req,res)=>{
// console.log("messages",req.params.id);

try {
    const {message} = req.body;
    const receiverId = req.params.id;
    const senderId = req.user._id;
    console.log(`Sender: ${senderId}, Receiver: ${receiverId},, Message" ${message}`);

    let conversation = await Conversation.findOne({
        participants: { $all : [senderId, receiverId]}
    })

    //send 1st time
    if(!conversation){
        conversation = await Conversation.create({
            participants: [senderId,receiverId],
        })
    }

    const newMessage = new Message({
        senderId,
        receiverId,
        message
    })
    
    if(newMessage){
        conversation.messages.push(newMessage._id);

    }
     // await conversation.save();
    // await newMessage.save();
    //optimize the upper 2 line
    await Promise.all([conversation.save(), newMessage.save()])//this will run in parallel


    //Socket IO
    const receiverSocketId = getReceiverSocketId(receiverId);

	if (receiverSocketId) {
		// io.to(<socket_id>).emit() used to send events to specific client
		io.to(receiverSocketId).emit("newMessage", newMessage);
	}

    // console.log("newMessage", newMessage);
    res.status(201).json(newMessage)

    
} catch (error) {
    console.log("Error in send Message Route", error);
    res.status(500).send({
        success:false,
        message: error.message
    })
}

}

const getMessage = async(req,res)=>{
try {
const userToChatId = req.params.id;
const senderId = req.user._id;

    const conversation = await Conversation .findOne({
        participants:{$all : [senderId,userToChatId]},
    }).populate("messages")//not ref but actual message

    if(!conversation){
        return res.status(200).send({
            success:true,
            message: [],
        })
    }
    
    const messages  = conversation.messages
    res.status(200).send({
        success:true,
        message: messages
    })
} catch (error) {
    console.log("Error in Get Message Route", error);
    res.status(500).send({
        success:false,
        message: error.message
    })
}       

}

module.exports = {sendMessage,getMessage}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant