How to implement event listening in PHP

后端 未结 5 918
天命终不由人
天命终不由人 2020-12-08 12:22

here is my problem: I have a script (let\'s call it comet.php) whic is requsted by an AJAX client script and wait for a change to happen like this:

while(no         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 12:56

    As much as I like PHP, I must say that PHP isn't the best choice for this task. Node.js is much, much better for this kind of thing and it scales really good. It's also pretty simple to implement if you have JS knowledge.

    Now, if you don't want to waste CPU cycles, you have to create a PHP script that will connect to a server of some sort on a certain port. The specified server should listen for connections on the chosen port and every X amount of time check for whatever you want to check (db entries for new posts for example) and then it dispatches the message to every connected client that the new entry is ready.

    Now, it's not that difficult to implement this event queue architecture in PHP, but it'd take you literally 5 minutes to do this with Node.js and Socket.IO, without worrying whether it'll work in majority of browsers.

提交回复
热议问题