phptwitterstreamingdaemonsymfony-process

Sending back the output of a daemon through a socket


I'm creating a Twitter client, and I need to handle the tweets coming from each streaming processes (started with Symfony's Process component). I have a websocket server running in the background which works perfectly.

My problem is that I don't know how should I get the data streamed from these processes and pass back to the client through the socket. To keep checking an read the the output I need to create a while loop for it, but that will prevent other users to start their stream, because the socket server will stuck at that loop.

If possible, I want to avoid the using of DB queues.

The server, started from terminal:

class StreamServer extends \WebSocketServer
{
    /**
     *
     * Called immediately when the data is recieved.
     *
     * @param $user
     * @param $message
     */
    protected function process($user, $message)
    {
        var_dump($user);
    }

    /**
     * Called after the handshake response is sent to the client.
     *
     * @param $user
     */
    protected function connected($user)
    {
        $streamer = new Process('php Streamer.php');
        $streamer->setTimeout(null);
        $streamer->start();

        $this->send($user, 'tweets');
    }

    /**
     * Called after the connection is closed.
     *
     * @param $user
     */
    protected function closed($user)
    {
        $this->disconnect($user->socket);
    }
}

$echo = new StreamServer("127.0.0.1", "9000");

try {
    $echo->run();
} catch (Exception $e) {
    $echo->stdout($e->getMessage());
}

Streamer:

class Streamer
{
    public function run()
    {
//        $this->user = $user;
        echo 'hello';


        $config = [
            'oauth' => [
                'consumer_key'    => '**',
                'consumer_secret' => '**',
                'token'           => '**',
                'token_secret'    => '**',
            ],
        ];

        $floodgate = Floodgate::create($config);

        $handler = function ($message)
        {
            var_export($message);
        };

        $generator = function ()
        {
            return [];
        };

        $floodgate->user($handler, $generator);
    }
}

$stream = new Streamer();
$stream->run();

Solution

  • I found a solution, called ReactPHP & Ratchet.