- Socket.IO๋ฅผ ์ฌ์ฉํด ์๋ก์ด ๋๊ธ ์๋ฆผ ๊ธฐ๋ฅ์ ๊ตฌํํจ.
- ํด๋ผ์ด์ธํธ๊ฐ ๋๊ธ์ ๋จ๊ธธ ๋๋ง๋ค ์๋ฒ์์ ์ค์๊ฐ์ผ๋ก ์๋ฆผ์ ๋ณด๋ด๊ฒ ๋๋๋ฐ, ์ด๋ฅผ ํตํด ๊ฒ์๋ฌผ์ ๋๊ธ์ด ์ถ๊ฐ๋ ๋๋ง๋ค ๊ด๋ จ ์ ๋ณด๊ฐ ์ ์ก๋์ด ์ค์๊ฐ ์๋ฆผ์ ๋ณด๋ผ ์ ์์
1. ์๋ฒ ์ฝ๋
1) server.js ์ค์
- ์๋ฒ์์ Socket.IO๋ฅผ ์ด๊ธฐํํ๊ณ , ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๊ธฐ ์ํ ํ์ผ์ ๊ตฌ์ฑํจ
// server.js
const socketIo = require("socket.io");
let io;
function initSocket(server) {
io = socketIo(server);
io.on("connection", (socket) => {
console.log("A user connected");
socket.on("disconnect", () => {
console.log("User disconnected");
});
});
}
// emitComment ํจ์๋ io๊ฐ ์ด๊ธฐํ๋ ํ ํธ์ถ๋ ์ ์๋๋ก ์ค์
function emitComment(data) {
if (!io) {
console.error("Socket.io is not initialized.");
return;
}
console.log("Attempting to emit new-comment event...");
io.emit("new-comment", data); // ์ด๋ฒคํธ ๋ฐ์
console.log("Emitted new-comment event:", data); // ์ด๋ฒคํธ ๋ฐ์ ํ ๋ก๊ทธ ์ถ๊ฐ
}
module.exports = { initSocket, emitComment };
2) ๊ธฐ์กด์ Root index.js ์์
- ์๋ฒ๋ฅผ ์ด๊ธฐํํ๊ณ , Socket.IO ์๋ฒ๋ฅผ ์ค์ .
- server.js ํ์ผ์์ ๋ง๋ initSocket ํจ์๋ก Socket.IO๋ฅผ ์ด๊ธฐํํจ.
- http ํจํค์ง์์ Server ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ express ํจํค์ง์ธ app์ ๊ฐ์ธ์ฃผ๊ณ ์ด๋ฅผ ๋ ๋ค์ Socket.IO๋ฉ์๋๋ก ๊ฐ์ธ์ค
// index.js
const express = require("express");
const http = require("http");
...
// Socket.IO ์ฐ๊ฒฐ, initSocket ํจ์๋ฅผ ๊ฐ์ ธ์ด
const { initSocket } = require("./server");
// ๋ผ์ฐํฐ
...
const app = express();
const server = http.createServer(app); // HTTP ์๋ฒ ์์ฑ
// MongoDB ์ฐ๊ฒฐ
...
// ๋ฏธ๋ค์จ์ด
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// ๋ผ์ฐํธ ๋ฑ๋ก
...
// ์๋ฒ ์ด๊ธฐํ
initSocket(server);
// ์๋ฒ ์์
server.listen(3000, () => {
console.log(`Server is running on port 3000`);
});
module.exports = app;
3) ๊ธฐ์กด์ createComment.js ์์ (์ด๋ฒคํธ ๋ฐ์)
- ๋๊ธ์ด ์ถ๊ฐ๋๋ฉด emitComment ํจ์๊ฐ ํธ์ถ๋์ด Socket.IO๋ฅผ ํตํด ์๋ก์ด ๋๊ธ ์ ๋ณด๋ฅผ ์ ์ก.
// createComment.js
const { emitComment } = require("../../server"); // server.js์ emit ํจ์ ์ฌ์ฉ
router.post("/:postId", auth, async (req, res) => {
const { text } = req.body;
const postId = req.params.postId;
const userId = req.user._id;
try {
const post = await Post.findById(postId);
if (!post) {
return res.status(404).json({ message: "๊ฒ์๋ฌผ์ ์ฐพ์ ์ ์์ต๋๋ค." });
}
const newComment = new Comment({
post: postId,
user: userId,
text: text,
createdAt: Date.now(),
});
await newComment.save();
await Post.findByIdAndUpdate(postId, { $push: { comments: newComment._id } });
emitComment({
postId: postId,
commentId: newComment._id,
commentText: newComment.text,
commenterId: userId,
});
return res.status(201).json({
message: "๋๊ธ์ด ์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐ๋์์ต๋๋ค.",
comment: newComment,
});
} catch (error) {
return res.status(500).json({
message: "๋๊ธ ์ถ๊ฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.",
error: error.message,
});
}
});
module.exports = router;
2. ํด๋ผ์ด์ธํธ ์ฝ๋ (์์)
- ์์ง ํ๋ก ํธ์ชฝ ๊ตฌํ์ ์ํด์ ๊ธฐ์กด์ Postman์ ํ์ฉํ๋ ค๊ณ ํ๋๋ฐ, Postman์ ์ฌ์ฉํ ํ ์คํธ๋ ๋ถ๊ฐ๋ฅํ์
- ํฐ๋ฏธ๋์ Socket.IO ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ์์ฑํ์ฌ ์ด๋ฒคํธ๊ฐ ์๋ฒ์์ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ก๋๋์ง ํ์ธํ์.
- ํด๋ผ์ด์ธํธ ๊ธฐ๋ณธ ์ธํ
// client.js
const io = require("socket.io-client");
// ์๋ฒ์์ ์์ผ ์ฐ๊ฒฐ
const socket = io("http://localhost:3000"); // ์๋ฒ URL์ ๋ง๊ฒ ์์
// ์๋ฒ์ ์ฐ๊ฒฐ ์ฑ๊ณต ์
socket.on("connect", () => {
console.log("์๋ฒ์ ์ฐ๊ฒฐ๋์์ต๋๋ค.");
});
// ์๋ฒ์์ "new-comment" ์ด๋ฒคํธ ์์
socket.on("new-comment", (data) => {
console.log("์๋ก์ด ๋๊ธ์ด ์ถ๊ฐ๋์์ต๋๋ค!");
console.log(data); // ์๋ฒ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ ์ถ๋ ฅ
});