Using comet with PHP?

2018-12-31 05:07发布

I was thinking of implementing real time chat using a PHP backend, but I ran across this comment on a site discussing comet:

My understanding is that PHP is a terrible language for Comet, because Comet requires you to keep a persistent connection open to each browser client. Using mod_php this means tying up an Apache child full-time for each client which doesn’t scale at all. The people I know doing Comet stuff are mostly using Twisted Python which is designed to handle hundreds or thousands of simultaneous connections.

Is this true? Or is it something that can be configured around?

标签: php comet
11条回答
素衣白纱
2楼-- · 2018-12-31 05:31

I'm having a similar issue. One option I'm finding interesting is to use an existing Comet server, like cometd-java or cometd-python, as the core message hub. Your PHP code is then just a client to the Comet server -- it can post or read messages from channels, just like other clients.

There's an interesting code snippet linked here: http://morglog.org/?p=22=1 that implements part of this method (although there are bits of debug code spread around, too).

查看更多
其实,你不懂
3楼-- · 2018-12-31 05:31

You'll have a hard time implementing comet in PHP, just because of it's inherent single-threaded-ness.

Check out Websync On-Demand - the service lets you integrate PHP via server-side publishing, offloading the heavy concurrent connection stuff, and will let you create a real-time chat app in no time.

查看更多
看淡一切
4楼-- · 2018-12-31 05:31

You will have to create your own server in PHP. Using Apache/mod_php or even fastcgi will not scale at all. A few years old, but can get you started:

PHP-Comet-Server: http://sourceforge.net/projects/comet/

查看更多
只若初见
5楼-- · 2018-12-31 05:34

I'm current implementing a scalable PHP Comet server using socket functions. It is called 'phet' ( [ph]p com[et] )

Project page: http://github.com/Tim-Smart/phet

Free free to join in on development. I have currently managed to get most of the server logic done, just need to finish off the client side stuff.

EDIT: Recently added 'Multi-threading' capabilities using the pcntl_fork method :)

查看更多
只靠听说
6楼-- · 2018-12-31 05:35

You could use Nginx and JavaScript to implement a Comet based chat system that is very scalable with little memory or CPU utilization.

I have a very simple example here that can get you started. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.

http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/

查看更多
不再属于我。
7楼-- · 2018-12-31 05:35

You may also try https://github.com/reactphp/react

React is a low-level library for event-driven programming in PHP. At its core is an event loop, on top of which it provides low-level utilities, such as: Streams abstraction, async dns resolver, network client/server, http client/server, interaction with processes. Third-party libraries can use these components to create async network clients/servers and more.

The event loop is based on the reactor pattern (hence the name) and strongly inspired by libraries such as EventMachine (Ruby), Twisted (Python) and Node.js (V8).

The introductory example shows a simple HTTP server listening on port 1337:

<?php

$i = 0;

$app = function ($request, $response) use (&$i) {
    $i++;

    $text = "This is request number $i.\n";
    $headers = array('Content-Type' => 'text/plain');

    $response->writeHead(200, $headers);
    $response->end($text);
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);

$http->on('request', $app);

$socket->listen(1337);
$loop->run();
查看更多
登录 后发表回答