Backend ๐Ÿ“š/Node.js

[Node]Instagram Clone - 12. Socket.io๋ฅผ ํ™œ์šฉํ•œ ์ƒˆ๋กœ์šด ๋Œ“๊ธ€ ๋“ฑ๋ก ์•Œ๋ฆผ ๊ธฐ๋Šฅ ๊ตฌํ˜„

leejaejae 2024. 11. 9. 14:46
  • 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); // ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
});

 

3. ๊ฒฐ๊ณผ ๐ŸŽ‰