Development Tip

Long-Polling, Websockets, SSE (Server-Sent Events) 및 Comet은 무엇입니까?

yourdevel 2020. 9. 27. 14:06
반응형

Long-Polling, Websockets, SSE (Server-Sent Events) 및 Comet은 무엇입니까?


나는 몇몇 기사를 읽으려고 시도했지만 아직 개념에 대해 명확하지 않습니다.

누군가가 이러한 기술이 무엇인지 설명해 주시겠습니까?

  1. 긴 폴링
  2. 서버에서 보낸 이벤트
  3. 웹 소켓
  4. 혜성

매번 접한 한 가지는 서버가 연결을 열어두고 데이터를 클라이언트로 푸시한다는 것입니다. 연결은 어떻게 열려 있으며 클라이언트는 푸시 된 데이터를 어떻게 얻습니까? (클라이언트가 데이터를 어떻게 사용합니까? 일부 코드가 도움이 될 수 있습니까?)

이제 실시간 앱에 어떤 것을 사용해야합니까? websockets (socket.io [node.js 라이브러리] 포함)에 대해 많이 들었지만 PHP가 아닌 이유는 무엇입니까?


아래 예에서 클라이언트는 브라우저이고 서버는 웹 사이트를 호스팅하는 웹 서버입니다.

이러한 기술을 이해하려면 먼저 클래식 HTTP 웹 트래픽 을 이해해야 합니다.

일반 HTTP :

  1. 클라이언트는 서버에서 웹 페이지를 요청합니다.
  2. 서버가 응답을 계산합니다.
  3. 서버는 클라이언트에 응답을 보냅니다.

HTTP

Ajax 폴링 :

  1. 클라이언트는 일반 HTTP를 사용하여 서버에서 웹 페이지를 요청합니다 (위의 HTTP 참조).
  2. 클라이언트는 요청 된 웹 페이지를 수신하고 정기적으로 (예 : 0.5 초) 서버에서 파일을 요청하는 페이지에서 JavaScript를 실행합니다.
  3. 서버는 일반 HTTP 트래픽과 마찬가지로 각 응답을 계산하여 다시 보냅니다.

Ajax 폴링

Ajax Long-Polling :

  1. 클라이언트는 일반 HTTP를 사용하여 서버에서 웹 페이지를 요청합니다 (위의 HTTP 참조).
  2. 클라이언트는 요청 된 웹 페이지를 수신하고 서버에서 파일을 요청하는 페이지에서 JavaScript를 실행합니다.
  3. 서버는 요청 된 정보로 즉시 응답하지 않고 사용 가능한 정보 가있을 때까지 기다립니다 .
  4. 사용 가능한 새 정보가 있으면 서버가 새 정보로 응답합니다.
  5. 클라이언트는 새 정보를 수신하고 즉시 서버에 다른 요청을 보내 프로세스를 다시 시작합니다.

Ajax 롱 폴링

HTML5 서버 전송 이벤트 (SSE) / EventSource :

  1. 클라이언트는 일반 HTTP를 사용하여 서버에서 웹 페이지를 요청합니다 (위의 HTTP 참조).
  2. 클라이언트는 요청 된 웹 페이지를 수신하고 서버에 대한 연결을 여는 페이지에서 JavaScript를 실행합니다.
  3. 서버는 사용 가능한 새로운 정보가있을 때 클라이언트에 이벤트를 보냅니다.

    • 서버에서 클라이언트로의 실시간 트래픽, 대부분 필요합니다.
    • 이벤트 루프가있는 서버를 사용하고 싶을 것입니다.
    • 다른 도메인의 서버와의 연결은 올바른 CORS 설정 에서만 가능합니다.
    • 더 많은 것을 읽고 싶다면 (article) , (article) , (article) , (tutorial) 이 매우 유용하다는 것을 알았습니다 .

HTML5 SSE

HTML5 웹 소켓 :

  1. 클라이언트는 일반 http를 사용하여 서버에서 웹 페이지를 요청합니다 (위의 HTTP 참조).
  2. 클라이언트는 요청 된 웹 페이지를 수신하고 서버와의 연결을 여는 페이지에서 JavaScript를 실행합니다.
  3. 이제 서버와 클라이언트는 새 데이터 (양쪽)를 사용할 수있을 때 서로 메시지를 보낼 수 있습니다.

    • 서버에서 클라이언트로 실시간 트래픽 클라이언트에서 서버로
    • 이벤트 루프가있는 서버를 사용하고 싶을 것입니다.
    • With WebSockets it is possible to connect with a server from another domain.
    • It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to implement the client side, which is very easy!
    • If you want to read more, I found these very useful: (article), (article) (tutorial).

HTML5 WebSockets

Comet:

Comet is a collection of techniques prior to HTML5 which use streaming and long-polling to achieve real time applications. Read more on wikipedia or this article.


Now, which one of them should I use for a realtime app (that I need to code). I have been hearing a lot about websockets (with socket.io [a node.js library]) but why not PHP ?

You can use PHP with WebSockets, check out Ratchet.


Tieme put a lot of effort into his excellent answer, but I think the core of the OPs question is how these technologies relate to PHP rather than how each technology works.

PHP is the most used language in web development besides the obvious client side html, css, and javascript. Yet PHP has 2 major issues when it comes to real time applications:

1) PHP started as a very basic CGI. PHP has progressed very far since it's early stage, but it happened in small steps. PHP already had many millions of users by the time it became the embed-able and flexible C library that it is today, most of whom were dependent on it's earlier model of execution, so it hasn't yet made a solid attempt to escape the cgi model internally. Even the commandline interface invokes the PHP library (libphp5.so on linux, php5ts.dll on windows, etc) as if it still a cgi processing a GET/POST request. It still executes code as if it just has to build a "page" and then end it's life cycle. As a result, it has very little support for multi-thread or event driven programming (within PHP userspace), making it currently unpractical for real time, multi-user applications.

Note that PHP does have extensions to provide event loops (such as libevent) and threads (such as pthreads) in PHP userspace, but very, very, few of the applications use these.

2) PHP still has significant issues with garbage collection. Although these issues have been consistently improving (likely it's greatest step to end the life cycle as described above), even the best attempts at creating long running PHP applications require being restarted on a regular basis. This also make it unpractical for real time applications.

PHP 7 will be a great step to fix these issues as well, and seems very promising as a platform for real-time applications.


I have tried to make note about these and have collected and written examples from a java perspective.

HTTP for Java Developers

Reverse Ajax - Old style

서버 측의 비동기 처리

Reverse Ajax-새로운 스타일

서버 전송 이벤트

동일한 주제를 찾고있는 모든 자바 개발자를 위해 여기에 배치합니다.


실시간 커뮤니케이션을 위해서만 웹 앱에서 Node.JS를 쉽게 사용할 수 있습니다. Node.JS는 WebSocket에 대해 정말 강력합니다. 따라서 "Node.js를 통한 PHP 알림"은 훌륭한 개념이 될 것입니다.

이 예제를 참조하십시오 : PHP 및 Node.js를 사용하여 실시간 채팅 앱 만들기

참고 URL : https://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet

반응형