웹 소켓
웹 소켓이란?
-
기존의 단방향 HTTP 프로토콜과 호환되어 양방향 통신을 제공하기 위해 개발된 프로토콜이다.
- 일반 Socket 통신과는 달리, HTTP 80 Port를 사용하므로 방화벽에 제약이 없다.
- 접속까지는 HTTP 프로토콜을 이용하고, 그 이후 통신은 자체적인 WebSocket 프로토콜로 통신한다.
-
HTTP는 HTML이라는 문서를 운반하기 위한 프로토콜이다.
- 모든 HTTP를 사용한 통신은 클라이언트가 먼저 요청을 보내고, 그 요청에 따라 웹 서버가 응답하는 형태이며, 웹 서버는 응답을 보낸 후 웹 브라우저와의 연결을 끊는다.
- 양쪽이 데이터를 동시에 보내는 것이 아니기 때문에, 이러한 통신 방식을 반이중 통신이라고 한다.
- HTTP를 이용한 정보 송수신은 클라이언트의 요청이 없다면, 서버로부터 응답을 받을 수 없는 구조이다.
-
웹 소켓에서는 서버와 브라우저 사이에 양방향 통신이 가능하다.
- 클라이언트가 먼저 요청하지 않아도 서버가 먼저 데이터를 보낼 수도 있고, 사용자가 다른 웹사이트로 이동하지 않아도 최신 데이터가 적용된 웹을 볼 수 있게 해준다.
HTTP에서 실시간 보장
→ WebSocket이 존재하기 전에는 Polling이나 Long Polling, Streaming 등의 방식으로 해결했다.
Polling
- 클라이언트가 평범한 HTTP Request를 서버로 계속 요청해 이벤트 내용을 전달받는 방식이다.
- 가장 쉬운 방법이지만 클라이언트가 지속적으로 Request를 요청하기 때문에 클라이언트의 수가 많아지면 서버의 부담이 급증한다.
- HTTP Request Connection을 맺고 끊는 것 자체가 부담이 많은 방식이고, 클라이언트에서 실시간 정도의 빠른 응답을 기대하기 어렵다.
Long Polling