Chat features added
This commit is contained in:
parent
773e175288
commit
b3561bd155
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/node_modules
|
/node_modules
|
||||||
|
.env
|
@ -13,10 +13,13 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"crypto-js": "^4.2.0",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.21.1",
|
"express": "^4.21.1",
|
||||||
"socket.io": "^4.8.0"
|
"socket.io": "^4.8.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/crypto-js": "^4.2.2",
|
||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
"@types/node": "^22.7.5",
|
"@types/node": "^22.7.5",
|
||||||
"@types/socket.io": "^3.0.2",
|
"@types/socket.io": "^3.0.2",
|
||||||
|
Binary file not shown.
41
src/index.ts
41
src/index.ts
@ -2,6 +2,7 @@ import express, { Request, Response } from "express";
|
|||||||
import { createServer, Server as HTTPServer } from "http";
|
import { createServer, Server as HTTPServer } from "http";
|
||||||
import { Server as SocketIOServer, Socket } from "socket.io";
|
import { Server as SocketIOServer, Socket } from "socket.io";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
|
require("dotenv").config();
|
||||||
|
|
||||||
// Initialize Express
|
// Initialize Express
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -9,7 +10,8 @@ const app = express();
|
|||||||
// Set up CORS middleware for Express
|
// Set up CORS middleware for Express
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
origin: "https://realtime-voicecall.vercel.app", // Update this to the origin of your client
|
origin:
|
||||||
|
process.env.NEXT_FRONTEND_URL || "https://realtime-voicecall.vercel.app", // Update this to the origin of your client
|
||||||
methods: ["GET", "POST"],
|
methods: ["GET", "POST"],
|
||||||
credentials: true, // Allow credentials (e.g., cookies) if needed
|
credentials: true, // Allow credentials (e.g., cookies) if needed
|
||||||
})
|
})
|
||||||
@ -22,7 +24,8 @@ const httpServer: HTTPServer = createServer(app);
|
|||||||
const io = new SocketIOServer(httpServer, {
|
const io = new SocketIOServer(httpServer, {
|
||||||
path: "/socket", // Define the WebSocket path
|
path: "/socket", // Define the WebSocket path
|
||||||
cors: {
|
cors: {
|
||||||
origin: "https://realtime-voicecall.vercel.app", // Update this to the origin of your client
|
origin:
|
||||||
|
process.env.NEXT_FRONTEND_URL || "https://realtime-voicecall.vercel.app", // Update this to the origin of your client
|
||||||
methods: ["GET", "POST"],
|
methods: ["GET", "POST"],
|
||||||
credentials: true, // Allow credentials (e.g., cookies) if needed
|
credentials: true, // Allow credentials (e.g., cookies) if needed
|
||||||
},
|
},
|
||||||
@ -56,6 +59,8 @@ io.on("connection", (socket: Socket) => {
|
|||||||
socket.on("disconnect", () => {
|
socket.on("disconnect", () => {
|
||||||
onlineUsers.delete(socket.id);
|
onlineUsers.delete(socket.id);
|
||||||
io.emit("online-users", Array.from(onlineUsers.values()));
|
io.emit("online-users", Array.from(onlineUsers.values()));
|
||||||
|
io.emit("user-diconnected", socket.id);
|
||||||
|
console.log("User Disconnected", socket.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle call-related events
|
// Handle call-related events
|
||||||
@ -72,6 +77,38 @@ io.on("connection", (socket: Socket) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on("send-message", (message) => {
|
||||||
|
const { recipentSocketId } = message;
|
||||||
|
|
||||||
|
// Emit message to the specific recipient
|
||||||
|
const recipient = onlineUsers.get(recipentSocketId);
|
||||||
|
console.log("recipientSocketId:", recipient, recipentSocketId, onlineUsers);
|
||||||
|
if (recipient) {
|
||||||
|
io.to(recipentSocketId).emit("receive-message", message);
|
||||||
|
console.log("Message Received:", message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("send-user-typing", (user) => {
|
||||||
|
const { recipentSocketId } = user;
|
||||||
|
|
||||||
|
// Emit message to the specific recipient
|
||||||
|
const recipient = onlineUsers.get(recipentSocketId);
|
||||||
|
if (recipient) {
|
||||||
|
io.to(recipentSocketId).emit("receive-user-typing");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on("send-user-typing-end", (user) => {
|
||||||
|
const { recipentSocketId } = user;
|
||||||
|
|
||||||
|
// Emit message to the specific recipient
|
||||||
|
const recipient = onlineUsers.get(recipentSocketId);
|
||||||
|
if (recipient) {
|
||||||
|
io.to(recipentSocketId).emit("receive-user-typing-end");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
socket.on("call-accepted", (data: { to: string }) => {
|
socket.on("call-accepted", (data: { to: string }) => {
|
||||||
const caller = onlineUsers.get(socket.id);
|
const caller = onlineUsers.get(socket.id);
|
||||||
socket.to(data.to).emit("call-accepted", { caller });
|
socket.to(data.to).emit("call-accepted", { caller });
|
||||||
|
15
yarn.lock
15
yarn.lock
@ -79,6 +79,11 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@types/node" "*"
|
"@types/node" "*"
|
||||||
|
|
||||||
|
"@types/crypto-js@^4.2.2":
|
||||||
|
version "4.2.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.2.2.tgz#771c4a768d94eb5922cc202a3009558204df0cea"
|
||||||
|
integrity sha512-sDOLlVbHhXpAUAL0YHDUUwDZf3iN4Bwi4W6a0W0b+QcAezUbRtH4FVb+9J4h+XFPW7l/gQ9F8qC7P+Ec4k8QVQ==
|
||||||
|
|
||||||
"@types/express-serve-static-core@^5.0.0":
|
"@types/express-serve-static-core@^5.0.0":
|
||||||
version "5.0.0"
|
version "5.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-5.0.0.tgz#91f06cda1049e8f17eeab364798ed79c97488a1c"
|
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-5.0.0.tgz#91f06cda1049e8f17eeab364798ed79c97488a1c"
|
||||||
@ -259,6 +264,11 @@ create-require@^1.1.0:
|
|||||||
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
|
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
|
||||||
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
|
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
|
||||||
|
|
||||||
|
crypto-js@^4.2.0:
|
||||||
|
version "4.2.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.2.0.tgz#4d931639ecdfd12ff80e8186dba6af2c2e856631"
|
||||||
|
integrity sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==
|
||||||
|
|
||||||
debug@2.6.9:
|
debug@2.6.9:
|
||||||
version "2.6.9"
|
version "2.6.9"
|
||||||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
||||||
@ -297,6 +307,11 @@ diff@^4.0.1:
|
|||||||
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
|
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
|
||||||
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
|
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
|
||||||
|
|
||||||
|
dotenv@^16.4.5:
|
||||||
|
version "16.4.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
|
||||||
|
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==
|
||||||
|
|
||||||
ee-first@1.1.1:
|
ee-first@1.1.1:
|
||||||
version "1.1.1"
|
version "1.1.1"
|
||||||
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
|
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user