|
|
@ -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 }); |
|
|
|