-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7870300
commit b3aa5b9
Showing
62 changed files
with
748 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Request, Response } from "express"; | ||
import { myDataSource } from "../db"; | ||
import userEntity from "../models/user.entity"; | ||
import { ApiError, ApiResponse } from "../utils"; | ||
|
||
const getAllUsers = async (req: Request, res: Response) => { | ||
try { | ||
const users = await myDataSource.getRepository(userEntity).find({ | ||
select: { | ||
name: true, | ||
id: true, | ||
socketId: true, | ||
imgUrl: true | ||
} | ||
}); | ||
|
||
if(!users) throw new ApiError(400, "Error fetching data!") | ||
|
||
res | ||
.status(200) | ||
.json(new ApiResponse(200, users, "Fetched all users!")); | ||
} catch (error: any) { | ||
res.status(error.statusCode | 500).json({ | ||
success: false, | ||
message: error.message, | ||
}); | ||
} | ||
}; | ||
|
||
|
||
const setUserSocketId = async (req: Request, res: Response) => { | ||
try { | ||
const { email, socketId } = req.body | ||
|
||
const user = await myDataSource.getRepository(userEntity).findOne({ | ||
where: { | ||
email: email | ||
} | ||
}) | ||
|
||
if (!user) throw new ApiError(401, "User does not exists!") | ||
|
||
const isUpdated = await myDataSource.getRepository(userEntity).save({ | ||
...user, | ||
socketId: socketId, | ||
onlineStatus: "true" | ||
}) | ||
|
||
if (!isUpdated) throw new ApiError(500, "User status update failed!") | ||
|
||
res.status(200).json(new ApiResponse(200, [], "User status update success!")) | ||
|
||
} catch (error: any) { | ||
res.status(error.statusCode | 500).json({ | ||
success: false, | ||
message: error.message, | ||
}) | ||
} | ||
}; | ||
|
||
|
||
const setUserOffline = async (req: Request, res: Response) => { | ||
try { | ||
const { email} = req.body | ||
|
||
const user = await myDataSource.getRepository(userEntity).findOne({ | ||
where: { | ||
email: email | ||
} | ||
}) | ||
|
||
if (!user) throw new ApiError(401, "User does not exists!") | ||
|
||
const isUpdated = await myDataSource.getRepository(userEntity).save({ | ||
...user, | ||
onlineStatus: "false" | ||
}) | ||
|
||
if (!isUpdated) throw new ApiError(500, "User status update failed!") | ||
|
||
res.status(200).json(new ApiResponse(200, [], "User status update success!")) | ||
|
||
} catch (error: any) { | ||
res.status(error.statusCode | 500).json({ | ||
success: false, | ||
message: error.message, | ||
}) | ||
} | ||
}; | ||
|
||
export { getAllUsers, setUserOffline, setUserSocketId } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { app } from "./app"; | ||
import { server } from "./app"; | ||
import dotenv from "dotenv"; | ||
import connectDB from "./db"; | ||
import { createServer } from "http"; | ||
import { io } from "./socket"; | ||
|
||
dotenv.config({ | ||
path: "../.env", | ||
}); | ||
|
||
connectDB() | ||
.then(() => { | ||
app.listen(process.env.PORT || 6969, () => { | ||
server.listen(process.env.PORT || 6969, () => { | ||
console.log(`⚙️ Server is running at port : ${process.env.PORT}`); | ||
}); | ||
}) | ||
.then(() => { | ||
io.listen(5001) | ||
}) | ||
.catch((err) => { | ||
console.log("POSTGRES db connection failed !!! ", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { EntitySchema } from "typeorm"; | ||
|
||
export default new EntitySchema({ | ||
name: "Socket Id and User Id Map", | ||
tableName: "sid_map", | ||
columns: { | ||
sid: { | ||
type: "text" | ||
}, | ||
uid: { | ||
type: "text", | ||
primary: true | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import express from "express"; | ||
import { check } from "express-validator"; | ||
import validateRequest from "../middlewares/validReq.middleware"; | ||
import { | ||
getAllUsers, | ||
setUserOffline, | ||
setUserSocketId, | ||
} from "../controllers/user.controller"; | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/getAllUsers", getAllUsers); | ||
|
||
router.post( | ||
"/setUserSocketId", | ||
[check("email").notEmpty().isEmail(), check("socketId").notEmpty()], | ||
validateRequest, | ||
setUserSocketId | ||
); | ||
|
||
router.post( | ||
"/setUserOffline", | ||
[check("email").notEmpty().isEmail()], | ||
validateRequest, | ||
setUserOffline | ||
) | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Server } from "socket.io"; | ||
import { server } from "./app"; | ||
import dotenv from "dotenv"; | ||
import { Message } from "./types/message.types"; | ||
import deleteUserFromSocketList from "./utils/lib/deleteUserFromSocketList"; | ||
|
||
dotenv.config(); | ||
|
||
const io = new Server(server, { | ||
cors: { | ||
origin: process.env.CLIENT_URL, | ||
methods: ["GET", "POST"], | ||
credentials: true, | ||
}, | ||
}); | ||
|
||
interface User { | ||
name: string; | ||
email: string; | ||
uid: string; | ||
sid: string; | ||
} | ||
|
||
var connectedUsers: User[] = []; | ||
|
||
io.use((socket, next) => { | ||
//TODO verify user request | ||
//TODO save or replace user socket id to database | ||
next(); | ||
}); | ||
|
||
io.on("connection", (socket) => { | ||
io.to(socket.id).emit("user-sid", {sid: socket.id}) | ||
|
||
socket.on("user-details", (user) => { | ||
connectedUsers = deleteUserFromSocketList(connectedUsers, user); | ||
connectedUsers.push(user); | ||
io.emit("user-joined", connectedUsers); | ||
}); | ||
|
||
socket.on("message", (msg: Message) => { | ||
console.log(msg); | ||
if (msg.receipient.sid.length === 0) return; | ||
socket.to(msg.receipient.sid).emit("receive-message", msg); | ||
//TODO: Asynchronously save message to db | ||
console.log("emitted msg to respective client"); | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
const updatedUserList: User[] = []; | ||
connectedUsers.forEach((user) => { | ||
if (user.sid !== socket.id) updatedUserList.push(user); | ||
}); | ||
connectedUsers = updatedUserList | ||
socket.disconnect() | ||
io.emit("user-left", connectedUsers); | ||
}); | ||
}); | ||
|
||
export { io }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface Message { | ||
msg_id: string; | ||
sender: User; | ||
receipient: User; | ||
content: string; | ||
timestamp: Date; | ||
} | ||
|
||
interface User { | ||
name: string; | ||
id: string; | ||
sid: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
interface User { | ||
name: string; | ||
email: string; | ||
uid: string; | ||
sid: string; | ||
} | ||
|
||
function deleteUserFromSocketList(list: User[], user: User) { | ||
const newList: User[] = []; | ||
if (list.length > 0) { | ||
list.forEach((usr) => { | ||
if (usr.name !== user.name) newList.push(usr); | ||
}); | ||
} | ||
return newList; | ||
} | ||
|
||
export default deleteUserFromSocketList; |
Oops, something went wrong.