Answer the question
In order to leave comments, you need to log in
How to organize data exchange between PHP applications?
Hi all!
It is necessary to organize data exchange between two PHP applications, there are sooo many requests (100 requests per second), while their volume is very small - it is important to organize it in such a way that it would work at the speed of light.
In general, there are two PHP applications - you need to make a super fast exchange of small commands between them, like go; stop; read and so on
now it's all implemented using the usual http get, can anyone have any ideas how to speed it up?
Application 1 is based on Windows, running a PHP script from the command line, in which the command request is looped:
//циклим бесконечность
while (true){
//циклим до получения команды
while (!$command){
//запрашиваем команду
$command=file_get_contents("http://site.com/command.php");
}
//обрабатываем команду
if(command=="get_app_status"){
....... делаем некое действие соответсвующее команде (читаем txt файл)
$result=$text;
//возвращаем результат
echo $result;
}
}
Answer the question
In order to leave comments, you need to log in
Well, you could open a socket on the daemon and listen to it, and from the second application, drop connections accordingly.
This will be faster in any way than using a web server via http.
Alternatively, you can implement a message queue service.
Gearman for example.
There is an excellent WAMP socket implementation in PHP called Ratchet. Detailed documentation on this site
And now a recipe from practice:
1. Building a simple application using composer, we need the cboden / ratchet package
2. Writing the base class
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Msg implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
}
public function onMessage(ConnectionInterface $from, $msg) {
}
public function onClose(ConnectionInterface $conn) {
}
public function onError(ConnectionInterface $conn, \Exception $e) {
}
}
<?php
use Ratchet\Server\IoServer;
use MyApp\Msg;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new Msg(),
8080
);
$server->run();
<?php
require __DIR__ . '/vendor/autoload.php';
\Ratchet\Client\connect('ws://192.168.1.100:8080')->then(function($conn) {
$conn->on('message', function($msg) use ($conn) {
echo "Received: {$msg}\n";
$conn->close();
});
$conn->send('Hello World!');
}, function ($e) {
echo "Could not connect: {$e->getMessage()}\n";
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question