diff --git a/src/services/chatService.js b/src/services/chatService.js index 39d11c2..32445a2 100644 --- a/src/services/chatService.js +++ b/src/services/chatService.js @@ -65,8 +65,9 @@ const sendMessage = async (sender, receiverUsername, message) => { } } + let savedChat, savedMessage; try { - const [savedChat, savedMessage] = await Promise.all([chat.save(), newMessage.save()]); + [savedChat, savedMessage] = await Promise.all([chat.save(), newMessage.save()]); } catch (error) { return { err: { status: 500, message: 'An error occurred while saving the chat or message' } }; } @@ -74,7 +75,7 @@ const sendMessage = async (sender, receiverUsername, message) => { const receiverSocketId = getReceiverSocketId(receiver._id); if (receiverSocketId) { // io.to().emit() used to send events to specific client - io.to(receiverSocketId).emit("newMessage", newMessage); + io.to(receiverSocketId).emit("newMessage", savedMessage); } return { newMessage }; diff --git a/src/socket/socket.js b/src/socket/socket.js index de690a9..9aed7ab 100644 --- a/src/socket/socket.js +++ b/src/socket/socket.js @@ -1,6 +1,9 @@ import { Server } from "socket.io"; import http from "http"; import express from "express"; +import { stat } from "fs"; +import jwt from "jsonwebtoken"; +import { User } from "../db/models/User.js"; // This line creates a new Express application. const app = express(); @@ -13,14 +16,13 @@ const server = http.createServer(app); // This line creates a new Socket.IO server that uses the HTTP server. // It also sets up Cross-Origin Resource Sharing (CORS) to allow requests from "http://localhost:3000" using the GET and POST methods. -// const io = new Server(server, { -// cors: { -// origin: ["http://localhost:2998", "http://localhost:2999", "http://localhost:3000"], -// methods: ["GET", "POST", "PUT", "PATCH","DELETE"], -// }, -// }); +const io = new Server(server, { + cors: { + origin: "*", + methods: ["GET", "POST", "PUT", "PATCH", "DELETE"], + }, +}); -const io = new Server(server) // This function is exported so it can be used in other files. // It takes a receiverId and returns the corresponding socket ID from userSocketMap. @@ -30,22 +32,45 @@ export const getReceiverSocketId = (receiverId) => { // This object maps user IDs to socket IDs. // It's used to keep track of which socket belongs to which user. -const userSocketMap = {}; // {userId: socketId} +const userSocketMap = {}; // {user_id: socketId} // This sets up an event listener for the "connection" event, which is emitted whenever a client connects to the server. // Inside the event listener, it logs the socket ID, // stores the socket ID in userSocketMap if the user ID is defined, // and sets up an event listener for the "disconnect" event. -io.on("connection", (socket) => { +io.on("connection", async (socket) => { console.log("a user connected", socket.id); - const userId = socket.handshake.query.userId; - if (userId != "undefined") userSocketMap[userId] = socket.id; + // Get the user from the procided token to fill the userSocketMap. + + // Extract the token from the query parameter. + const token = socket.handshake.query.token.split(" ")[1]; + + // Verify the token. + let user_token; + + try { + user_token = jwt.verify(token, process.env.JWT_SECRET); + } catch (err) { + console.log({ err: { status: 401, message: `Invalid Token: ${err.message}` } }); + } + + // Get the user id from the token. + const user_id = user_token._id; + + // Get the user from the id. + const user = await User.findById(user_id); + + if (!user) { + console.log({ err: { status: 404, message: "User not found" } }); + } else { + userSocketMap[user_id] = socket.id; + } // socket.on() is used to listen to the events. can be used both on client and server side socket.on("disconnect", () => { console.log("user disconnected", socket.id); - delete userSocketMap[userId]; + delete userSocketMap[user_id]; }); });