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

working chat NTH #57

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions peerprep/backend/collab-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions peerprep/backend/collab-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
]
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"mongoose": "^8.7.3",
"node-static": "^0.7.11",
"socket.io": "^4.8.1",
"ws": "^7.4.2",
"y-websocket": "^1.3.9",
"yjs": "^13.4.12"
Expand Down
30 changes: 28 additions & 2 deletions peerprep/backend/collab-service/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const setupWSConnection = ywsUtils.setupWSConnection;
const docs = ywsUtils.docs;
const connectDB = require('../config/db');
const { storeDocument, getDocument } = require('./controller/collab-controller');
const { Server } = require("socket.io");
const cors = require("cors");

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

app.use(cors());
app.use(express.json());

// Connect to MongoDB
Expand Down Expand Up @@ -49,7 +52,30 @@ wss.on('connection', (conn, req) => {
setupWSConnection(conn, req, { gc: req.url.slice(1) !== 'ws/prosemirror-versions' });
});

const io = new Server(server, {
cors: {
origin: "http://localhost:5173",
methods: ["GET", "POST"],
},
});

io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);

socket.on("join_room", (data) => {
socket.join(data);
});

socket.on("send_message", (data) => {
socket.in(data.room).emit("receive_message", data); // Send to all clients except sender
});

socket.on("disconnect", () => {
console.log(`User Disconnected: ${socket.id}`);
});
});

const PORT = process.env.PORT || 1234;
server.listen(PORT, () => {
console.log(`Server is running at http://localhost:1234`);
});
console.log(`Server is running at http://localhost:${PORT}`);
});
2 changes: 1 addition & 1 deletion peerprep/backend/question-service/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT=8080
MONGODB_URI = "mongodb+srv://common:T4mRbfIFytNk4xuU@questionservice.true3.mongodb.net/?retryWrites=true&w=majority&appName=QuestionService"
MONGODB_URI = "mongodb+srv://common:T4mRbfIFytNk4xuU@questionservice.true3.mongodb.net/?retryWrites=true&w=majority&appName=QuestionService"
Loading
Loading