오늘은 프로젝트 진행에 있고 백과 프론트에서 공통으로 사용할 기술 스택에 대해 조사를 진행했다.
우리는 socket 통신을 기반으로 실시간 비디오 / 채팅이 가능한 게임을 구현하기로 했기 때문에, 통신에 필요한 기술을 검색하고 어떤 것이 우리 프로젝트에 어울릴 지에 대해 논의했다.
TCP 통신
Node.js 내장 모듈인 http vs https 모듈 비교
- The difference between HTTP and HTTPS is if you need to communicate with the servers over SSL, encrypting the communication using a certificate, you should use HTTPS, otherwise you should use HTTP.
What is difference between NodeJS http and https module?
I am using http module in my project but most of my 'post' requests are blocked by postman. I read it is a ssl issue,after some research i found another module named https. Here is my current co...
stackoverflow.com
HTTPS | Node.js v19.3.0 Documentation
HTTPS# Source Code: lib/https.js HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a separate module. Determining if crypto support is unavailable# It is possible for Node.js to be built without including support for the node:crypt
nodejs.org
- (궁금한 점) https 모듈을 사용해서 서버를 열면, EC2에서 별도의 ssl 인증과 reverse proxy 설정을 하지 않아도 되는지??
http vs net 모듈
http.createServer() sets up a server that handles the HTTP protocol, which is indeed transmitted over tcp. net.createServer() creates a server that simply understands when a TCP connection has happened, and data has been transmitted, and so on, but doesn't know anything about whether a valid HTTP request has been received, etc.
If you are writing a web server, favor http.createServer() over net.createServer() as it will save you a lot of work. If you are writing some other kind of server, do not use http.createServer().
http.createserver vs net.createserver in node.js
I am having trouble understanding the difference between net.createserver and http.createserver in node.js. I have read the documentation for both methods located at these two urls https://nodejs...
stackoverflow.com
Socket 통신
- ws
- 장점 : 다른 모든 라이브러리보다 메모리 사용 측면에서 가장 유리함
- 단점 : 클라-서버 사이의 데이터를 주고 받을 때, 변환 작업을 직접 해줘야 함 (string만 주고 받을 수 있음…)
- socket.io
- socket.io를 사용해야하는 이유는??
- UDP 통신이 가능하지 않은 브라우저에서도 TCP long-polling 방식으로 양방향 통신을 구현
- TypeScript 지원
- 공식 홈페이지에 충분한 documentation 활용 가능
- socket.io를 사용해야하는 이유는??
- dgram
- UDP datagram socket 통신을 가능하게 해주는 node.js의 내장 모듈
- UDP 통신의 경우, 한번에 전송할 수 있는 패킷의 크기가 정해져 있으며, 한번의 전송에 전체 패킷을 담아야 함
- 전송하려는 패킷 단위 데이터의 크기가 limit을 초과한다면, 이를 쪼개서 개별 패킷으로 보내야 하며, UDP 통신 특성상, 받는 쪽에서의
- dgram을 사용해야 하는 이유는??
- node 내장 모듈이므로, socket.io를 사용하는 것보다 앱이 가벼워 짐
- 그렇지만, 기본적으로 UDP 통신을 기반으로 하기 때문에, 데이터 유실의 위험으로 선택하지 않음
UDP/datagram sockets | Node.js v19.3.0 Documentation
UDP/datagram sockets# Source Code: lib/dgram.js The node:dgram module provides an implementation of UDP datagram sockets. import dgram from 'node:dgram'; const server = dgram.createSocket('udp4'); server.on('error', (err) => { console.error(`server error:\
nodejs.org
- 결론
- 데이터 수신의 무결성을 위해 TCP 기반에서 통신이 이뤄지는 socket.io를 기본으로 사용하는 것이 좋을 것 같음
- 비디오 콜 및 동시 접속자 수를 고려하여 socket.io + uws 조합으로 구현하는 것이 메모리 사용에 가장 최적화 될 것 같음

Memory usage | Socket.IO
The resources consumed by your Socket.IO server will mainly depend on:
socket.io
- webRTC
- 웹앱과 사이트가 중간자(서버) 없이 브라우저 간 오디오/영상 미디어/ 임의의 데이터를 교환할 수 있도록 하는 기술
- 최초에 브라우저들을 연결하기 위해서는 서버에 각 브라우저의 config & location 정보를 보냄 → 서버가 브라우저에 peer의 정보를 중개해주면, 브라우저간 데이터 교환을 할 때, 서버를 거치지 않고 수행
- 고려해야 할 점
- 사용자가 접속하는 브라우저마다 다른 코덱 및 기타 미디어 기능에 대한 지원 수준이 다르기 때문에, Adapter.js 라이브러리 사용을 권장 (npm 라이브러리로도 제공)
- adapter 라이브러리는 shim과 polyfill을 사용해서 여러 플래폼에서 WebRTC 구현시 많은 차이점을 없애줌
- WebRTC는 기본적으로 peer-to-peer 연결이므로, 서버에 사용자의 데이터를 저장해야 하는 경우, socket.io를 통해 서버로 데이터를 보내야 함
- 만약 플레이어의 발표 세션을 저장하고 싶다면, 이런 경우에는, 비디오&오디오 스트리밍 데이터를 서버로 전송해서 socket 통신을 해야 하고, db에 해당 정보를 저장해야 함
- 사용자가 접속하는 브라우저마다 다른 코덱 및 기타 미디어 기능에 대한 지원 수준이 다르기 때문에, Adapter.js 라이브러리 사용을 권장 (npm 라이브러리로도 제공)
- 웹앱과 사이트가 중간자(서버) 없이 브라우저 간 오디오/영상 미디어/ 임의의 데이터를 교환할 수 있도록 하는 기술
WebRTC API - Web API | MDN
WebRTC(Web Real-Time Communication)은 웹 애플리케이션과 사이트가 중간자 없이 브라우저 간에 오디오나 영상 미디어를 포착하고 마음대로 스트림할 뿐 아니라, 임의의 데이터도 교환할 수 있도록 하는
developer.mozilla.org
(추가 개념) TCP vs UDP & socket
- ws와 socket.io는 모두 TCP 위에서 이뤄지는 socket 통신이며, dgram은 UDP 통신 기반의 socket 통신임
- UDP
- UDP (User Datagram Protocol) is message-based.
- A UDP message (datagram) is always guaranteed to be received as a whole (or not at all, as UDP doesn't guarantee delivery) from recvfrom() or whatever function you use to read UDP data from a socket. Note that this limits the practical maximum size of a UDP packet to about 512 bytes.
- UDP doesn't guarantee the order in which two separate UDP packets are received, or whether the packets are received at all.Note that if UDP was stream-based, and the UDP message was split into two packets (datagrams), and the receiver first received the latter half of the message and then the first half of the message, then it would be difficult to stitch together the full message. The receiver might even only receive one half of the message, i.e., drop a packet.
- TCP
- Unlike UDP, TCP is stream-based. It can split a single message into multiple packets because it guarantees the order in which packets are received. The receiver always receives the first sent packet first, and only then the second packet, making it easier for the receiver to stitch together the full message.
- WebSocket
- WebSocket runs on top of TCP, but is message-based, like UDP. Therefore WebSocket takes care of preserving message boundaries, like UDP does, and you don't need to parse together a full WebSocket message from multiple separate packets.
WebRTC API - Web API | MDN
WebRTC(Web Real-Time Communication)은 웹 애플리케이션과 사이트가 중간자 없이 브라우저 간에 오디오나 영상 미디어를 포착하고 마음대로 스트림할 뿐 아니라, 임의의 데이터도 교환할 수 있도록 하는
developer.mozilla.org
'항해99_10기 > 105일의 TIL & WIL' 카테고리의 다른 글
| [9주차] [WIL] 2023.1.6 ~ 1.12 실전프로젝트 2주차 회고 (0) | 2023.01.16 |
|---|---|
| [8주차] [20220103] nest.js 맛보기 (0) | 2023.01.04 |
| [WIL 7주차] 2022.12.23 ~ 2022.12.29 회고 (feat. 노션 클론코딩 완료 후기) (1) | 2023.01.01 |
| [8주차] [20221231] 실전 프로젝트 기획 (0) | 2023.01.01 |
| [7주차] [20221229] 1주일 notion clone 챌린지 회고 (0) | 2022.12.30 |