Socket Communitcation Reference - dnwls16071/Backend_Summary GitHub Wiki

๐Ÿ“š HTTP vs WebSocket

  • In HTTP and REST, an application is modeled as many URLs. To interact with the application, clients access those URLs, request-response style. Servers route requests to the appropriate handler based on the HTTP URL, method, and headers.
  • By contrast, in WebSockets, there is usually only one URL for the initial connect. Subsequently, all application messages flow on that same TCP connection. This points to an entirely different asynchronous, event-driven, messaging architecture.
  • WebSocket is also a low-level transport protocol, which, unlike HTTP, does not prescribe any semantics to the content of messages. That means that there is no way to route or process a message unless the client and the server agree on message semantics.
  • WebSocket clients and servers can negotiate the use of a higher-level, messaging protocol (for example, STOMP), through the Sec-WebSocket-Protocol header on the HTTP handshake request. In the absence of that, they need to come up with their own conventions.

๐Ÿ“š HTTP Protocol Architecture(Request - Response)

๐Ÿ“š WebSocket Architecture

  • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ์‹ค์‹œ๊ฐ„ ์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์„ ๊ฐ€๋Šฅ์ผ€ ํ•˜๋Š” ํ†ต์‹  ํ”„๋กœํ† ์ฝœ
  • ํด๋ผ์ด์–ธํŠธ โ†’ ์„œ๋ฒ„๋กœ์˜ ์•„๋‹ˆ๋ผ ํด๋ผ์ด์–ธํŠธ์˜ Request์—†์ด๋„ ์„œ๋ฒ„ โ†’ ํด๋ผ์ด์–ธํŠธ๋กœ ์‹ค์‹œ๊ฐ„ ์ „์†ก์ด ๊ฐ€๋Šฅ
  • HTTP ํ†ต์‹ ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ws://์˜ url์„ ๊ฐ€์ง„ ์›น ์†Œ์ผ“ ์ „์šฉ ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉ
  • STOMP ๋ฉ”์‹œ์ง€ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ws:// ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉ

๐Ÿ“š Long Polling vs WebSocket vs SSE

WebSocket๊ณผ Long Polling

  • Polling
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ์ฃผ๊ธฐ์ ์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋Š”๊ฐ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹
    • HTTP ์š”์ฒญ๋งŒ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€ ์„ค์ •์ด ํ•„์š”์—†๋‹ค.
    • ํ—ˆ๋‚˜ ๋ฐ˜๋Œ€๋กœ ๋งํ•˜๋ฉด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์—†๋Š”๋ฐ ๊ณ„์† Request๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋‚ญ๋น„๋  ์ˆ˜ ์žˆ๋‹ค.
  • WebSocket
    • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ์ง€์†์ ์ธ ์—ฐ๊ฒฐ ์œ ์ง€
    • ๋ฌด๊ฑฐ์šด HTTP ๋ฉ”์‹œ์ง€๊ฐ€ ํ•„์š”์—†์ด ์ง์ ‘ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ธฐ์— ์„œ๋ฒ„ ๋ถ€ํ•˜ ์ธก๋ฉด์—์„œ HTTP์— ๋น„ํ•˜๋ฉด ์šฐ์ˆ˜
  • SSE(Server Sent Events)
    • HTTP๊ธฐ๋ฐ˜์˜ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ์‹ค์‹œ๊ฐ„ ๋‹จ๋ฐฉํ–ฅ ํ†ต์‹ ์„ ๊ฐ€๋Šฅ์ผ€ํ•˜๋Š” ์›น ๊ธฐ์ˆ 
    • SSE ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•œ ํ†ต์‹ ๋ฐฉํ–ฅ์ด ์„œ๋ฒ„ โ†’ ํด๋ผ์ด์–ธํŠธ๋กœ์˜ ๋‹จ๋ฐฉํ–ฅ ํ†ต์‹ 
    • ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ•œ ๊ณณ์—์„œ ์‚ฌ์šฉ(์ฃผ๋กœ ์•Œ๋ฆผ)