📜 ⬆️ ⬇️

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

Dklab Realplexor is a Comet-server that allows you to simultaneously hold hundreds of thousands of long-lived open HTTP connections with user browsers. The JavaScript code running in the browser subscribes to one or more Realplexor channels and hangs the handler to receive data. The server can at any time write a message to 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 a minimum load for the server.

Although the ideological inspirer of Realplexor was the previous project, dklab_multiplexor, the Realplexor code has practically nothing in common with it. That's why I decided to change the name. The capabilities of the products are also incomparable (see below), and the code size has increased 7 times.

The real-time trend is now quite actively developing in the West, and in it the product Tornado is particularly notable - an event-oriented web server in Python. True, Tornado is not so much a Comet-server, as a tool with which you can program “including” and a Comet-server. Keywords: Comet , Push Server, Long polling, JavaScript, XMLHttpRequest.
')
The main advantages of Realplexor are:

It is better to see once ...


I made a separate online sandbox to demonstrate the functionality of the new Realplexor, and what Comet servers are generally needed for (by the way, this is physically the same Realplexor daemon that my new startup RuTvit uses ). The sandbox implements something like a multi-channel chat: by logging in, you’ll get as if 2 independent “browsers” running on different computers.By default, there are 3 channels open on the page with the names Alpha, Beta and RuTvit. But, of course, you can close these channels and open new ones. Here, for example, a page with a single open channel called Habrahabr: http://rutvit.ru/realplexor/demo?ids=Habrahabr .



The sandbox demonstrates the following features of Realplexor: Below are excerpts from the code of this sandbox (up to layout and with adaptation for this article), illustrating the use of API Realplexor.

Code Listing 1: Interesting excerpts from the 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 the 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' ] ) ;

Installing Realplexor


After downloading dklab_realplexor.tar.gz, it can be installed as a Linux autorun 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 RuTvit


Realtime search and track online channel status. When viewing search results, tweets that match the query appear on top in real time (as in Google and Bing's Twitter searches, as well as in FriendFeed). To provide this functionality, the server must at every moment have information about “active searches”, which is ensured by the corresponding PHP API function.

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

Subscribe to many channels at the same time. In the browse mode a la FriendFeed , when messages are grouped into branches, each branch is a subscription to a separate channel of Realplexor (ie, 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, for example, Like / Unlike (or retweet), private messages, etc. are implemented.

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

Transmission of messages of arbitrary structure. RuTvit PHP script generates Realplexor messages as nested associative arrays that are transparently converted to JavaScript objects on the browser side.

What's next?


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

Official project page

Source: https://habr.com/ru/post/79189/


All Articles