Skip to content

๐Ÿš€ Socket.io ์ตœ(๊ฐ•)์ ํ™”

Kontae edited this page Dec 3, 2024 · 1 revision

Web socket์— ๊ด€ํ•œ ๊ฐ„๋‹จํ•œ ๊ฐœ๋… ์ •๋ฆฌ๋ถ€ํ„ฐ

TCP ๊ธฐ๋ฐ˜์˜ ์–‘๋ฐฉํ–ฅ ์†ก์ˆ˜์‹ ์˜ ํ”„๋กœํ† ์ฝœ ์ด๋‹ค.
OSI Layer์—์„œ๋Š” HTTP์™€ ๊ฐ™์€ 7๊ณ„์ธต์— ์œ„์น˜๋ฅผ ํ•˜๊ณ  ์žˆ๊ณ  ์—ฐ๊ฒฐ์ด ์œ ์ง€๋œ๋‹ค๋Š” ์ ์—์„œ ํฐ ์žฅ์ ์ด ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ์ด๋‹ค.

ํŠน์ง•์œผ๋กœ๋Š”

  • ์–‘๋ฐฉํ–ฅ ์†ก์ˆ˜์‹  ๊ฐ€๋Šฅ (full duplex)
  • ์‹ค์‹œ๊ฐ„ ๋„คํŠธ์›Œํ‚น ๊ฐ€๋Šฅ (Real-time networking)
  • ๋‚ฎ์€ ์ง€์—ฐ์‹œ๊ฐ„ (low latency)

Socket.io๋ฅผ ์ด์šฉํ•œ ์ฑ„ํŒ…๋ฐฉ ๊ตฌํ˜„

image

Socket.IO์˜ room ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์„ ๋…๋ฆฝ์ ์œผ๋กœ ๊ด€๋ฆฌํ•จ์œผ๋กœ์จ, ๋ฐฉ ์ฐธ์—ฌ์ž๋“ค ๊ฐ„์˜ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ณต์œ ๊ฐ€ ํ•ด๋‹น ๋ฐฉ ๋‚ด์—์„œ๋งŒ ์ด๋ฃจ์–ด์ง€๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ์ธํ„ฐํŽ˜์ด์Šค

@WebSocketGateway({
  namespace: 'rooms', // namespace ๋“ฑ๋ก => ws://localhost:3000/rooms ๋กœ ์ ‘๊ทผ
  cors: {
    origin: '*',
    methods: ['GET', 'POST'],
  },
})
export class RoomGateway{
 @WebSocketServer()
  server: Server; // server ๋“ฑ๋ก
 constructor() {} // ์™ธ๋ถ€ repository, service ๋“ฑ ์˜์กด์„ฑ ์ฃผ์ž…

 @SubscribeMessage('test_message') // 'test_message' ๋ผ๋Š” event name์œผ๋กœ ์‹๋ณ„
 async handleTest(
	@ConnectedSocket() client: Socket, // client : ํ˜„์žฌ ์‚ฌ์šฉ์ž ์ž์‹ ์˜ socket
	@MessageBody() data : { testId : string; }, // messageBody์— ๋‹ด์•„์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ emit
 ) {
  // ๋กœ์ง
 }

}
  • ๊ธฐ๋ณธ์ ์œผ๋กœ NestJS์˜ @WebSocketGateway๋ฅผ implementํ•˜์—ฌ ์‚ฌ์šฉ
  • RoomGateway ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ ์ „์†กํ•˜๋Š” WS message์— ๊ด€ํ•œ ๊ฐ๊ฐ์˜ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋ก ํ•ด์ฃผ๋Š” ๋ฐฉ์‹

Ver 0.1

Sequence Diagram

sequenceDiagram
    participant Client
    participant Server
    participant Repository

    rect rgb(200, 200, 200)
    Note over Client,Repository: ๊ธฐ์กด ๋ฐฉ์‹
    Client->>Server: Socket ์—ฐ๊ฒฐ (handshaking)
    Server->>Client: Socket ์—ฐ๊ฒฐ ์‘๋‹ต (handshaking)
    Client->>Server: joinRoom ์ด๋ฒคํŠธ ๋ฐœ์ƒ
    Server->>Repository: ๋ฐฉ ์ •๋ณด ์กฐํšŒ
    Repository-->>Server: ๋ฐฉ ์ •๋ณด ๋ฐ˜ํ™˜
    Server-->>Client: ๋ฐฉ ์ฐธ์—ฌ ๊ฒฐ๊ณผ ์‘๋‹ต
    end
Loading

๊ธฐ์กด ์ธํ„ฐํŽ˜์ด์Šค

์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋Š” ์ดˆ๊ธฐ์— ์šฐ์„ ์ ์œผ๋กœ handshaking ๊ณผ์ •์„ ์ง„ํ–‰์„ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ดํ›„๋กœ ์š”์ฒญ์— ๊ด€ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ง์ ‘ ํŠน์ • message๋ฅผ emit ํ•˜๊ณ  ์„œ๋ฒ„์—์„œ ํŠน์ • ๋ฉ”์„ธ์ง€์— ๊ด€ํ•œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์œผ๋กœ ์ง„ํ–‰์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์„œ๋ฒ„์—์„œ ๋“ฑ๋กํ•œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์žˆ์Šต๋‹ˆ๋‹ค

handleCreateRoom: โ€œcreateRoomโ€ message subscribe์„ ํ†ตํ•˜์—ฌ ๋ฐฉ ์ƒ์„ฑ์— ๊ด€ํ•œ ๋กœ์ง์„ ๋‹ด๋‹น

handleJoinRoom: โ€œjoinRoomโ€ message subscribe์„ ํ†ตํ•˜์—ฌ ์œ ์ €๊ฐ€ ์ƒ์„ฑ๋œ ๋ฐฉ์— ์ž…์žฅํ•˜๋Š” ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง์„ ๋‹ด๋‹น

handleLeaveRoom: โ€œleaveRoomโ€ message subscribe์„ ํ†ตํ•˜์—ฌ ์œ ์ €๊ฐ€ ๋ฐฉ์—์„œ ๋‚˜๊ฐ€๋Š” ๊ฒƒ์— ๊ด€ํ•œ ๋กœ์ง์„ ๋‹ด๋‹น

Ver 0.2

sequenceDiagram
    participant Client
    participant Server
    participant Repository
    rect rgb(200, 240, 200)
    Note over Client,Repository: ๋ณ€๊ฒฝ๋œ ๋ฐฉ์‹
    Client->>Server: Socket ์—ฐ๊ฒฐ (handshaking)
    activate Server
    Server->>Repository: ๋ฐฉ ์ •๋ณด ์ฆ‰์‹œ ์กฐํšŒ
    Repository-->>Server: ๋ฐฉ ์ •๋ณด ๋ฐ˜ํ™˜
    Server-->>Client: ์—ฐ๊ฒฐ ์™„๋ฃŒ (handshaking)
    deactivate Server
    end
Loading

ํ˜„์žฌ ์ธํ„ฐํŽ˜์ด์Šค

๋‹ค๋งŒ inear ๋‚ด๋ถ€์—์„œ ์†Œ์ผ“ ์—ฐ๊ฒฐ์€ ํฌ๊ฒŒ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ƒˆ๋กœ์šด ๋ผ์ด๋ธŒ ์„ธ์…˜์— ๋“ค์–ด๊ฐˆ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ธฐ์— ํด๋ผ์ด์–ธํŠธ์˜ joinRoom ๊ณผ์ •์€ ์–ธ์ œ๋‚˜ handshaking ๊ณผ ํ•จ๊ป˜ ๋ฐ”๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ธฐ์— ์ €ํฌ๋Š” ์„œ๋ฒ„์˜ ์†Œ์ผ“ ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ํ๋ฆ„์„ ํ•ฉ์ณ์„œ ๋กœ์ง์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์ง€์—ฐ์‹œ๊ฐ„์„ ๋” ์ค„์ด๊ณ ์ž ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

handleConnection : Socket ์—ฐ๊ฒฐ ์‹œ์ ์— ์‚ฌ์šฉ์ž์˜ ๋ฐฉ ์ฐธ์—ฌ๋ฅผ ๊ฒ€์ฆํ•˜๊ณ  ์ดˆ๊ธฐํ™”

handleDisconnect : Socket ์—ฐ๊ฒฐ์ด ์ข…๋ฃŒ๋  ๋•Œ ์‚ฌ์šฉ์ž์˜ ๋ฐฉ ํ‡ด์žฅ ์ฒ˜๋ฆฌ ๋ฐ ๊ด€๋ จ๋œ ์ •๋ฆฌ ์ž‘์—…

์—ฐ๊ฒฐ ๊ณผ์ • ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด ๊ธฐ๋Œ€๋˜๋Š” ์ 

  • ์—ฐ๊ฒฐ ์ƒ๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ๋ฅผ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ๋ˆ„๋ฝ ์—†์ด ๋ชจ๋“  ์—ฐ๊ฒฐ/์ข…๋ฃŒ ์ƒํ™ฉ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • ๋„คํŠธ์›Œํฌ ๋ถˆ์•ˆ์ •์ด๋‚˜ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ์—ฐ๊ฒฐ ์ข…๋ฃŒ ์ƒํ™ฉ๋„ ์•ˆ์ •์ ์œผ๋กœ ์ฒ˜๋ฆฌ
  • ์—ฐ๊ฒฐ ์‹œ์ ์— ํ•„์š”ํ•œ ๋ชจ๋“  ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ
  • ๋ถˆํ•„์š”ํ•œ ์—ฐ๊ฒฐ์„ ์กฐ๊ธฐ์— ์ฐจ๋‹จํ•˜์—ฌ ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค ์ ˆ์•ฝํ•˜๊ณ , ์ฝ”๋“œ์˜ ์–‘๋„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
  • ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ๊ฐ€ ๋ช…ํ™•ํ•จ
    • ์—ฐ๊ฒฐ ๊ด€๋ฆฌ๋Š” handleConnection
    • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ๋ฉ”์‹œ์ง€ ํ•ธ๋“ค๋Ÿฌ
    • ์—ฐ๊ฒฐ ๋Š๋Š” ์ž‘์—…์€ handleDisconnect

inear

๊ธฐ์ˆ  ๊ณต์œ 

๐Ÿš€ ffmpeg๋Š” stderr๋กœ ๋””๋ฒ„๊น…์„ ํ•˜๋Š” ์ด์œ 
๐Ÿš€ HLS ํ”„๋กœํ† ์ฝœ์— ๊ด€ํ•œ ์ •๋ฆฌ ๋ฐ FFmpeg ์‚ฌ์šฉ๊ธฐ
๐Ÿš€ ๋น„ํŠธ๋Š” tsconfig.json์ด ์„ธ ๊ฐœ?
๐Ÿš€ NestJS ๊ธฐ๋ณธ ๊ฐœ๋… - Modules
๐Ÿš€ Socket.io ์ตœ(๊ฐ•)์ ํ™”
๐Ÿš€ ๋„์ปค์™€ nginx์˜ ์‚ฌ์šฉ๊ธฐ
๐Ÿš€ ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์ž

๊ฐœ๋ฐœ ์ผ์ง€

๐Ÿš€ FSD ์‚ฌ์šฉ๊ธฐ, ๊ทผ๋ฐ ์ด์ œ ๋‚˜๋งŒ์˜ ๊ทœ์น™์„ ๊ณ๋“ค์ธ
๐Ÿš€ CICD ๊ตฌ์กฐ ์ˆ˜์ •
๐Ÿš€ ์•จ๋ฒ” ๋‹จ์œ„๋กœ ์ŠคํŠธ๋ฆฌ๋ฐ ํ•˜๊ธฐ (with HLS)
๐Ÿš€ HLS๋กœ ์Œ์•… ์ฃผ๊ณ ๋ฐ›๊ธฐ
๐Ÿš€ vite + react + typescript ํ™˜๊ฒฝ์—์„œ path alias ์„ค์ •
๐Ÿš€ React Scan์ด ๋ญ์ฃ ?
๐Ÿš€ ๋กœ์ปฌ ํ™˜๊ฒฝ ๊ฐœ๋ฐœ ๋ชจ๋“œ ๋ฐฐํฌ
๐Ÿš€ ์•จ๋ฒ” ์ „์ฒด๋ฅผ ์ŠคํŠธ๋ฆฌ๋ฐํ•œ๋‹ค๊ณ ? (with HLS)
๐Ÿš€ ์ฝ”๋“œ์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž

ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

๐Ÿš€ ์ƒˆ๋กœ๊ณ ์นจ ์‹œ HLS ERROR
๐Ÿš€ input ํƒœ๊ทธ์— ํ•œ๊ธ€ ์ž…๋ ฅ ํ›„, Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋‘๋ฒˆ ํ˜ธ์ถœ๋˜๋Š” ์˜ค๋ฅ˜
๐Ÿš€ nginx proxy pass๋ฅผ ๋ฐ”๊ฟจ๋”๋‹ˆ ์ƒ๊ธด ์—๋Ÿฌ - ์Šค์›จ๊ฑฐ ์ธ์‹ ๋ฌธ์ œ
๐Ÿš€ ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ WS handshake
๐Ÿš€ ๋ Œ๋”๋ง ๋ฒ”์ธ์€ ํ•˜๋‚˜!

ํ˜‘์—… ๊ทœ์น™

๐ŸŒˆ ๊ทธ๋ผ์šด๋“œ ๋ฃฐ
๐Ÿฅ” ํŒ€์› ์†Œ๊ฐœ
๐Ÿ”Ž ์ฝ”๋“œ & ๊นƒ ์ปจ๋ฒค์…˜
๐ŸŒณ ๊นƒ branch ์ „๋žต
๐Ÿ“Œ ๋…ธ์…˜ ๋ฌธ์„œ ์ €์žฅ์†Œ

ํ”„๋กœ์ ํŠธ ๊ธฐํš

๐ŸŽจ ํ”ผ๊ทธ๋งˆ
๐Ÿง‘โ€๐Ÿ’ป ๊ธฐํš ๊ณต์œ  ๋ฐœํ‘œ ์ž๋ฃŒ
๐ŸŽค 2์ฃผ์ฐจ ๋ฐœํ‘œ ์ž๋ฃŒ
๐Ÿ˜Ž ๋ฐฑ๋กœ๊ทธ

๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ

๐Ÿ“ 1์ฃผ์ฐจ
๐Ÿ“ 2์ฃผ์ฐจ
๐Ÿ“ 3์ฃผ์ฐจ
๐Ÿ“ 4์ฃผ์ฐจ
๐Ÿ“ 5์ฃผ์ฐจ

์ฃผ๊ฐ„ ๊ณ„ํš์„œ

๐Ÿ—“๏ธ 1์ฃผ์ฐจ
๐Ÿ—“๏ธ 2์ฃผ์ฐจ
๐Ÿ—“๏ธ 3์ฃผ์ฐจ
๐Ÿ—“๏ธ 4์ฃผ์ฐจ
๐Ÿ—“๏ธ 5์ฃผ์ฐจ

๊ทธ๋ฃน ํšŒ๊ณ 

โœจ 1์ฃผ์ฐจ
โœจ 2์ฃผ์ฐจ
โœจ 3์ฃผ์ฐจ
โœจ 4์ฃผ์ฐจ


view

Clone this wiki locally