Skip to content

Commit

Permalink
socket issue resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
BhanuArora123 committed Oct 27, 2022
1 parent 94691f1 commit 1043696
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Backend/controllers/roomsControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ exports.joinRoom = async (req, res, next) => {
let { roomId, userId } = req.body;
let roomData = await room.findOne({
roomId
});
})
.populate("users.userId","userName");
if (!roomData) {
return res.status(404).json({
message: "room not found"
Expand All @@ -84,7 +85,8 @@ exports.joinRoom = async (req, res, next) => {
await roomData.save();

return res.status(200).json({
message: "room joined successfully"
message: "room joined successfully",
roomData
})
} catch (error) {
return res.status(500).json({
Expand All @@ -99,7 +101,7 @@ exports.getAllRooms = async (req, res, next) => {
try {
let allRooms = await room
.find()
.populate("users.userId","userName")
.populate("users.userId","userName profile")
let newAllRooms = [];
for (let i = 0; i < allRooms.length; i++) {
const room = allRooms[i];
Expand Down
2 changes: 2 additions & 0 deletions Backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mongoose.connect(process.env.MONGO_URI)
.then(() => {
const PORT = process.env.PORT || 4000;
const server = app.listen(PORT);
console.log("db connected")
const io = createSocketConnection(server, {
cors: {
origin: "*"
Expand All @@ -48,6 +49,7 @@ mongoose.connect(process.env.MONGO_URI)

// socket join room event
socket.on("join_room",(roomId,userName) => {
console.log(roomId,userName);
socket.join(roomId);
socket.broadcast.to(roomId).emit("user_join",{
message:"a new user join the room!",
Expand Down
4 changes: 2 additions & 2 deletions Frontend/src/components/JoinCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const JoinCard = ({ roomId, name, room, noOfQuestions, range }) => {

const handleClick = (roomId) => {
console.log("clicked");
socket.emit("join_room", (roomId, user.handle));
console.log(roomId);
socket.emit("join_room", roomId, user.handle);
console.log("roomId - " + roomId,"user - " + user.handle );
navigate(`/room/${roomId}`)
};

Expand Down
3 changes: 2 additions & 1 deletion Frontend/src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export default function Home() {
},
});

socket.emit("join_room", (roomID, user.user.handle));
console.log("roomId" + roomID, "user " +user.user.handle);
socket.emit("join_room", roomID, user.user.handle);

setLoading(false);

Expand Down

0 comments on commit 1043696

Please sign in to comment.