What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

前端 未结 4 1306
后悔当初
后悔当初 2020-11-21 06:51

I have tried reading some articles, but I am not very clear on the concepts yet.

Would someone like to take a shot at explaining to me what these technologies are:

4条回答
  •  你的背包
    2020-11-21 07:15

    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.

提交回复
热议问题