-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⬆️ socket io initial setup completed
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
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
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,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; |