Browse Source

Chat features added

main
Sourya Banerjee 2 months ago
parent
commit
b3561bd155
5 changed files with 59 additions and 3 deletions
  1. +2
    -1
      .gitignore
  2. +3
    -0
      package.json
  3. BIN
      realtime-voicecall-backend.rar
  4. +39
    -2
      src/index.ts
  5. +15
    -0
      yarn.lock

+ 2
- 1
.gitignore View File

@ -1 +1,2 @@
/node_modules
/node_modules
.env

+ 3
- 0
package.json View File

@ -13,10 +13,13 @@
"description": "",
"dependencies": {
"cors": "^2.8.5",
"crypto-js": "^4.2.0",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"socket.io": "^4.8.0"
},
"devDependencies": {
"@types/crypto-js": "^4.2.2",
"@types/express": "^5.0.0",
"@types/node": "^22.7.5",
"@types/socket.io": "^3.0.2",


BIN
realtime-voicecall-backend.rar View File


+ 39
- 2
src/index.ts View File

@ -2,6 +2,7 @@ import express, { Request, Response } from "express";
import { createServer, Server as HTTPServer } from "http";
import { Server as SocketIOServer, Socket } from "socket.io";
import cors from "cors";
require("dotenv").config();
// Initialize Express
const app = express();
@ -9,7 +10,8 @@ const app = express();
// Set up CORS middleware for Express
app.use(
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"],
credentials: true, // Allow credentials (e.g., cookies) if needed
})
@ -22,7 +24,8 @@ const httpServer: HTTPServer = createServer(app);
const io = new SocketIOServer(httpServer, {
path: "/socket", // Define the WebSocket path
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"],
credentials: true, // Allow credentials (e.g., cookies) if needed
},
@ -56,6 +59,8 @@ io.on("connection", (socket: Socket) => {
socket.on("disconnect", () => {
onlineUsers.delete(socket.id);
io.emit("online-users", Array.from(onlineUsers.values()));
io.emit("user-diconnected", socket.id);
console.log("User Disconnected", socket.id);
});
// 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 }) => {
const caller = onlineUsers.get(socket.id);
socket.to(data.to).emit("call-accepted", { caller });


+ 15
- 0
yarn.lock View File

@ -79,6 +79,11 @@
dependencies:
"@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":
version "5.0.0"
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"
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:
version "2.6.9"
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"
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:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"


Loading…
Cancel
Save