βš’ Backend/Node.js

[Node.js] μ‹€μ‹œκ°„ μ±„νŒ…μ•± λ§Œλ“€μ–΄λ³΄κΈ° 1 - Websocket μ΄μš©ν•˜μ—¬ λ°±μ—”λ“œ κ΅¬ν˜„ν•˜κΈ° (feat. Websocket) (Build a real-time chatapp 1 - Implement backend with websocket)

Fomagran πŸ’» 2022. 7. 28. 05:25
728x90
λ°˜μ‘ν˜•

μ•ˆλ…•ν•˜μ„Έμš” Foma μž…λ‹ˆλ‹€.

 

μ˜€λŠ˜μ€ Node.jsμ—μ„œ Websocket을 μ΄μš©ν•˜μ—¬ μ‹€μ‹œκ°„ μ„œλ²„λ₯Ό κ΅¬ν˜„ν•΄ 보렀고 ν•©λ‹ˆλ‹€.

 

λ°”λ‘œ μ‹œμž‘ν• κ²Œμš”~

 

(λ°±μ—”λ“œ μ„œλ²„λ₯Ό μ΄ˆκΈ°ν™” ν•˜λŠ” 방법은 μƒλž΅ν•˜κ³  μ§„ν–‰ν•©λ‹ˆλ‹€. ν˜Ήμ‹œ λͺ¨λ₯΄μ‹œλŠ” 뢄듀은 μ—¬κΈ° μ—μ„œ 확인해 μ£Όμ„Έμš”!)

 


Tutorial

 


Install

 

express

 

κ°„λ‹¨ν•˜κ²Œ μ„œλ²„λ₯Ό λ§Œλ“€ 수 μžˆλ„λ‘ λ„μ™€μ£ΌλŠ” λͺ¨λ“ˆ

 

npm install express@4.18.1

 

cors

 

HTTP의 동일 좜처 정책을 ν•΄κ²°ν•˜κΈ° μœ„ν•œ λͺ¨λ“ˆ

 

npm install cors@2.8.5

 

 

nodemon

 

μ†ŒμŠ€ μ½”λ“œ λ³€κ²½ μ‹œ μžλ™μœΌλ‘œ μ„œλ²„κ°€ μž¬μ‹€ν–‰ λ˜λ„λ‘ λ„μ™€μ£ΌλŠ” λͺ¨λ“ˆ

 

npm install nodemon@2.0.14

 

socket.io

 

μ‹€μ‹œκ°„ μ„œλ²„λ₯Ό λ§Œλ“€ 수 μžˆλ„λ‘ λ„μ™€μ£ΌλŠ” λͺ¨λ“ˆ

 

npm install socket.io@4.5.1

 

Implemet a websocket

 

μ•„λž˜λŠ” μ‹€μ‹œκ°„ μ›Ήμ†ŒμΌ“μ„ λ§Œλ“€κΈ° μœ„ν•΄ ν•„μš”ν•œ λ³€μˆ˜λ“€μž…λ‹ˆλ‹€.

 

const express = require("express");
const app = express();
const http = require("http");
const WebSocket = require("ws");
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const port = 3001;

 

μš°μ„  μ›Ή μ†ŒμΌ“ μ„œλ²„λ₯Ό μ—°κ²°ν•΄ μ€λ‹ˆλ‹€.

 

wss.on("connection", (ws) => {
  ...

 

κ·Έ λ‹€μŒ messageλ₯Ό μˆ˜μ‹ ν•  수 μžˆλ„λ‘ μ„ΈνŒ…ν•΄ μ€λ‹ˆλ‹€.

 

	ws.on("message", (message) => {
    	...

 

μ›Ή μ„œλ²„ μ†ŒμΌ“μ˜ ν΄λΌμ΄μ–ΈνŠΈλ“€λ‘œλΆ€ν„° 받은 데이터λ₯Ό forEach ν•¨μˆ˜λ‘œ λͺ¨λ‘ μˆœνšŒν•©λ‹ˆλ‹€.

 

    wss.clients.forEach(function each(client) {
    	...

 

λ§Œμ•½ ν΄λΌμ΄μ–ΈνŠΈμ˜ μ€€λΉ„ μƒνƒœκ°€ μ›Ήμ†ŒμΌ“μ΄ OPEN인 μƒνƒœλΌλ©΄ μˆ˜μ‹ ν•œ 메세지λ₯Ό JSON으둜 νŒŒμ‹±ν•œ λ’€ νŒŒμ‹±ν•œ 데이터λ₯Ό λ‹€μ‹œ stringν™” μ‹œμΌœμ„œ ν΄λΌμ΄μ–ΈνŠΈμ— μ „μ†‘ν•©λ‹ˆλ‹€.

 

      if (client.readyState === WebSocket.OPEN) {
        let parse = JSON.parse(message);
        let stringify = JSON.stringify(parse);
        client.send(stringify);
      }

 

μ„œλ²„κ°€ 잘 μž‘λ™λ˜λŠ”μ§€ ν…ŒμŠ€νŠΈ ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

 

app.get("/", (req, res) => {
  res.send("Hello World!");
});

server.listen(port, () => console.log(`Server running on ${port}`));

 

μ•„λž˜μ™€ 같이 잘 μž‘λ™λ˜λŠ”κ΅°μš”.

 


index.js

 

const express = require("express");
const app = express();
const http = require("http");
const WebSocket = require("ws");
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const port = 3001;

wss.on("connection", (ws) => {
  ws.on("message", (message) => {
    wss.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        let parse = JSON.parse(message);
        let stringify = JSON.stringify(parse);
        client.send(stringify);
      }
    });
  });
});

app.get("/", (req, res) => {
  res.send("Hello World!");
});

server.listen(port, () => console.log(`Server running on ${port}`));

Source Code

 

 

GitHub - fomagran/ChattingAppFullStack

Contribute to fomagran/ChattingAppFullStack development by creating an account on GitHub.

github.com

 

 

728x90
λ°˜μ‘ν˜•