Realplexor: a productive Comet server with API for PHP and Javascript (realtime)

    Dklab Realplexor is a Comet server that allows you to simultaneously keep hundreds of thousands of long-lived open HTTP connections to user browsers. The JavaScript code running in the browser subscribes to one or more Realplexor channels and hangs the handler for data receipt. The server can at any time record a message in one of these channels, and it will be instantly transmitted to all subscribers (at least one, at least a thousand), in real time and with minimal load on the server.

    Although Realplexor’s ideological inspiration was the previous project, dklab_multiplexor, Realplexor’s code has practically nothing to do with it. Therefore, I decided to change the name. Product features are also not comparable (see below), and the code size has increased 7 times.

    Realtime-direction is now quite actively developing in the West, and the product Tornado - an event-oriented web server in Python is particularly distinguished in it. True, Tornado is not so much a Comet server as a tool with which you can program “including” a Comet server. Keywords: Comet , Push Server, Long polling, JavaScript, XMLHttpRequest.

    The main advantages of Realplexor:
    • ease of use: availability of API for JavaScript, API for PHP (in the future - for other languages);
    • ease of configuration;
    • wide functionality (either absent or not available directly in analogs).

    Better to see once ...


    I made a separate online sandbox to demonstrate the functionality of the new Realplexor and what Comet servers are generally for (by the way, this is physically the same Realplexor daemon that my new startup Rutwit uses ). The sandbox implements something like multichannel chat: when you enter, you’ll get as if 2 independent “browsers” running on different computers.
    • The top “browser” displays channels - new messages instantly appear in them as soon as someone sends them there on the server side. Of course, hundreds of thousands of users can view this page at the same time, and they will all see the same thing (implemented using the Realplexor JavaScript API). You can add new channels on the fly (subscription) or hide existing ones (unsubscribe).
    • The lower browser contains forms that allow you to add a message to an arbitrary channel by specifying its name. The AJAX form is sent to the server, and already there the PHP script writes the received text to Realplexor via the PHP API. (And yes, you can chat like that.)
    By default, 3 channels are opened on the page with the names Alpha, Beta, and RuTvit. But, of course, you can close these channels and open new ones. For example, here is a page with the only open channel named Habrahabr: http://rutvit.ru/realplexor/demo?ids=Habrahabr . The sandbox demonstrates the following Realplexor features:



    • Simultaneous listening to multiple channels with the dynamic addition / removal of channels.
    • Simultaneous sending a message to several channels (in fact, it turned out a chat).
    • Update on the fly the list of online channels (channels that at least one user listens to).
    • Using the JavaScript API and the PHP API that come with Realplexor.
    Below are excerpts from the code of this sandbox (accurate to typesetting and with adaptation for this article), illustrating the use of the Realplexor API.

    Code Listing 1: Interesting excerpts from sandbox code: JavaScript
    // Create Dklab_Realplexor client.
    var realplexor = new Dklab_Realplexor (
        "http://rpl.YourSite.com/", // Realplexor's engine URL; must be a sub-domain
        "demo_" // namespace
    );
     
    // Subscribe a callback to channel Alpha.
    realplexor.subscribe ("Alpha", function (result, id) {
        alert (result);
    });
     
    // Subscribe a callback to channel Beta.
    realplexor.subscribe ("Beta", function (result, id) {
        div.innerHTML = result;
    });
     
    // Apply subscriptions. Callbacks are called asynchronously on data arrival.
    realplexor.execute ();

    Code Listing 2: Interesting excerpts from sandbox code: PHP
    // Create new API object to access Realplexor server.
    require_once "Dklab / Realplexor.php";
    $ rpl = new Dklab_Realplexor ("127.0.0.1", "10010", "demo_");
    ...
    // Send data to channels. 
    $ rpl-> send (array ("Alpha", "Beta"), $ _POST ['message']);

    Install Realplexor


    After downloading dklab_realplexor.tar.gz, you can install it as an autorun Linux service:
    cd / opt
    wget http://github.com/DmitryKoterov/dklab_realplexor/tarball/master
    tar zxf * realplexor * .tar.gz
    mv * realplexor * / dklab_realplexor
     
    # Now deal with an init-script.
    ln -s /opt/dklab_realplexor/dklab_realplexor.init /etc/init.d/dklab_realplexor
    chkconfig --add dklab_realplexor
    chkconfig dklab_realplexor on
    service dklab_realplexor start

    How we use Realplexor in Rutwit


    Realtime-search and tracking the online status of the channel. When viewing search results, tweets that match the query appear on top in real time (as in Google and Bing twitter searches, as well as in FriendFeed). To provide this functionality, the server at each moment must have information about "active searches", which provides the corresponding function of the PHP API.

    Subscribe to the channel. When you look at any tweet feed (your own, public , etc. - throughout the site), the Realplexor channel is used for it. Therefore, a new tweet appears for all users simultaneously and without reloading the page.

    Subscribe to many channels at once. In view a la FriendFeedWhen messages are grouped into branches, each branch is a subscription to a separate Realplexor channel (i.e., the browser is subscribed to dozens and sometimes even hundreds of channels simultaneously in one connection). Multiple subscription is the key functionality of Realplexor: through it are implemented, for example, Like / Unlike (or retweets), private messages, etc.

    Sending a message to several channels at once. When a user tweeted , the message is sent simultaneously to his personal channel, to all his friends' home channels and to the public channel in one Realplexor request.

    Transfer of messages of any structure. The PHP script of Rutvita generates Realplexor messages in the form of nested associative arrays, which are transparently converted into JavaScript objects on the browser side.

    What's next?


    API description and examples can be found on the project page . I invite developers to test the product. I will be glad to any comments or suggestions for improving Realplexor, as well as forks on GitHub and patches in the mail.

    Project official page

    Also popular now: