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

Tagging and WebSocket #4

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WebSocket part added and Tagging implemented
  • Loading branch information
RiteshIIT committed Jun 5, 2024
commit 708ba5e2ef2c570fb65fc679d0e947d38bdd12c1
6 changes: 6 additions & 0 deletions api/controllers/infopostControllers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const asyncHandler = require("express-async-handler");
const dotenv = require("dotenv");
const axios=require('axios');
dotenv.config();
const infopostModel = require("../models/infopostModel");
const imageModel = require("../models/imageModel");
@@ -44,10 +45,15 @@ const postinfopost = asyncHandler(async (req, res) => {
savedImages.push(image.filename);
}
}
//defining tag for the question
const query=req.body.body;
const tag_response=await axios.post('http://localhost:5001/tag',{query});
const classified_tag=tag_response.data;
const infopost = new infopostModel({
body: req.body.body,
url: req.body.urls,
images: savedImages,
tag:classified_tag
});
const message = "Infopost posted successfully";
await infopost.save().then((data) => {
16 changes: 15 additions & 1 deletion api/controllers/questionController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
const axios= require('axios');
const dotenv = require("dotenv");
dotenv.config();
const asyncHandler = require("express-async-handler"); // the function of async handler here is to handle errors in async functions. https://www.npmjs.com/package/express-async-handler
@@ -88,6 +89,10 @@ const postQuestion = asyncHandler(async (req, res) => {
}
console.log("user found", um);
const message = "Question posted successfully";
//defining tag for the question
const query=body.body;
const tag_response=await axios.post('http://localhost:5001/tag',{query});
const classified_tag=tag_response.data;
//creating question
await questionModel
.create({
@@ -96,6 +101,7 @@ const postQuestion = asyncHandler(async (req, res) => {
body: body.body,
images: savedImages,
_id: qID,
tag:classified_tag
//reason I didn't initialise comment or answer is because it used to create a default answer and comment
})
.then(async (data) => {
@@ -109,8 +115,14 @@ const postQuestion = asyncHandler(async (req, res) => {
um.asked_questions = temp;
await um.save();

//creating tfidf embeddings
axios.get('http://localhost:5001/embed').then(
console.log('created embeddings')
)

res.json({data,message});
});

});
} catch (err) {
res.status(400).json({ message: "Error occured while posting the question" });
@@ -302,7 +314,8 @@ const answerQ = asyncHandler(async (req, res) => {

const body = req.body["answers"];
console.log("body", body);
const um = await userModel.findOne({ user_ID: body.user_ID });
// const um = await userModel.findOne({ user_ID: body.user_ID });
const um = await userModel.findOne({ user_ID: req.body.user_ID });
if(!um){
res.status(401).json({error:"User not found"})
}
@@ -348,6 +361,7 @@ const commentQ = asyncHandler(async (req, res) => {
const cID = new mongoose.Types.ObjectId();
const body = req.body["comments"];
const um = await userModel.findOne({ user_ID: body.user_ID });
// const um = await userModel.findOne({ user_ID: req.body.user_ID });
if(!um){
res.status(401).json({error:"User not found"})
}
1 change: 1 addition & 0 deletions api/models/infopostModel.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const infopostSchema = mongoose.Schema({
hidden: { type: Boolean, default: false },
images: [{ type: String }],
asked_At: { type: Date, default: Date.now },
tag: { type: String, required: true }
});

const infopostModel = mongoose.model("infopostModel", infopostSchema);
1 change: 1 addition & 0 deletions api/models/questionModel.js
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ const QuestionSchema = mongoose.Schema({
answers: [AnswerSchema],
comments: [commentSchema],
verified: { type: Boolean, default: false },
tag: { type: String, required: true }
});

//create model of the schema
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -47,6 +47,9 @@ app.use(
//for future versions
//app.use(prefix + "/search", require("./api/routes/elasticRouters"));

//WebSocket Server
require('./webSocket/websocketServer')

//listening to port 5000 by default
app.listen(port, () =>
console.log(
23 changes: 23 additions & 0 deletions webSocket/websocketHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const axios=require('axios');

const handleWebSocketConnection=(ws)=>{
console.log('WebSocket Client Connected');

ws.on('message',async(message)=>{
console.log(`Recieved message from client : ${message}`);
try{
const response = await axios.post('http://localhost:5001/recommend', { message });
ws.send(JSON.stringify(response.data));
} catch(error){
console.error('Error fetching recommendations:', error);
ws.send(JSON.stringify({error:'Error fetching recommendations'}));
}
});

ws.on('close',()=>{
console.log('WebSocket Client Disconnected')
});
};

module.exports=handleWebSocketConnection;

18 changes: 18 additions & 0 deletions webSocket/websocketServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const http=require('http');
const express=require('express');
const app=express();
const WebSocket =require('ws');
const handleWebSocketConnection=require('./websocketHandler');

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

wss.on('connection',handleWebSocketConnection)

server.listen(3000, () => {
console.log('WebSocket Server is running on http://localhost:3000');
});

module.exports = server;