Skip to content

Commit

Permalink
⬆️ socket io initial setup completed
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 28, 2024
1 parent 05037b2 commit 3a6b24d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.5.3",
"nodemailer": "^6.9.14",
"socket.io": "^4.8.0",
"stripe": "^16.12.0",
"uuid": "^10.0.0",
"winston": "^3.14.2",
Expand Down
8 changes: 7 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express from "express";
import { createServer } from "http";
import { connectDB } from "./config/connectDB";
import routes from "./presentation/routers/index";
import cors from "cors";
Expand All @@ -7,10 +8,13 @@ import helmet from "helmet";
import bodyParser from "body-parser";
import { webhook } from "./presentation/routers/appointment/AppointmentRoutes";
import { CLIENT_URL, NODE_ENV, PORT } from "./config/env";
import initializeSocket from "./presentation/socket";

const port = PORT || 8080;

const app = express();
const server = createServer(app);

app.use(helmet());
app.use(
cors({
Expand All @@ -27,9 +31,11 @@ app.use(express.urlencoded({ extended: true }));
app.use("/api", routes);

connectDB().then(() => {
app.listen(port, () => {
server.listen(port, () => {
if (NODE_ENV !== "production") {
console.log(`Server start listening on port: ${port}`);
}
});
});

initializeSocket(server);
47 changes: 47 additions & 0 deletions server/src/presentation/socket/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Server } from "socket.io";
import { Server as HTTPServer } from "http"; // Type from the HTTP server
import { CLIENT_URL } from "../../config/env";
import logger from "../../utils/logger";

const initializeSocket = (server: HTTPServer) => {
const io = new Server(server, {
cors: {
origin: CLIENT_URL,
methods: ["GET", "POST"],
credentials: true,
},
});

// Listen for new connections
io.on("connection", (socket) => {
console.log(`User connected: ${socket.id}`);

// Handle joining a room
socket.on("join-room", (roomId) => {
socket.join(roomId);
logger.info(`User ${socket.id} joined room ${roomId}`);
});

// Handle offering WebRTC signal (from one client to another)
socket.on("offer", (offer, roomId) => {
socket.to(roomId).emit("offer", offer);
});

// Handle answering WebRTC offer
socket.on("answer", (answer, roomId) => {
socket.to(roomId).emit("answer", answer);
});

// Handle ICE candidates for WebRTC
socket.on("ice-candidate", (candidate, roomId) => {
socket.to(roomId).emit("ice-candidate", candidate);
});

// Handle disconnection
socket.on("disconnect", () => {
logger.warn(`User disconnected: ${socket.id}`);
});
});
};

export default initializeSocket;

0 comments on commit 3a6b24d

Please sign in to comment.