1

Question, I'm just wondering if I can call a mongoose schema inside the socket.on function? Like

socket.on('sendMessage', ({ senderId, receiverId, message }) => {
    const user = getUser(receiverId);
  const accountId = await accountModel.findById(senderId);
    if (user) {
      socket.to(user.socketId).emit('getMessage', {
        accountId,
        message,
      });
    }
  });

Thanks!

rSeeker
  • 161
  • 10

1 Answers1

0

You probably can have it, just make sure the .on callback receives an async function on it. Something like:

socket.on('sendMessage', async ({ senderId, receiverId, message }) => {
    const user = getUser(receiverId);
    const accountId = await accountModel.findById(senderId);
    if (user) {
      socket.to(user.socketId).emit('getMessage', {
        accountId,
        message,
      });
    }
  });