I have a WebSocket server (modified Ratchet) that uses ZermoMQ for pushing messages to the server. Recently I've migrated the server onto a new VPS.
The implementation is identical but for some reason when I request a PHP script over HTTP, then ZeroMQ doesn't send the payload to the WebSocket server but when I run the script through the terminal, it works.
This is the script that gets new requests to be sent to Websocket Server:
<?php
if(isset($_POST['identity'], $_POST['identifier'], $_POST['env'], $_POST['action'], $_POST['payload'])){
$identity = $_POST['identity'];
$identifier = $_POST['identifier'];
$env = $_POST['env'];
$action = $_POST['action'];
$payload = $_POST['payload'];
$context = new ZMQContext();
$client = $context->getSocket(ZMQ::SOCKET_PUSH, 'ICU push');
$client->connect('tcp://127.0.0.1:21002');
$client->send(json_encode(array('identity' => $identity, 'env' => $env, 'message' => formatRequest($identifier, $action, $payload))));
} else {
echo "Invalid request";
exit;
}
And the server start script:
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../autoload.php');
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Wamp\WampServer;
use ICU\Server;
use ICU\Logger;
$logger = new Logger();
$logger->setInteractive(true);
$loop = React\EventLoop\Factory::create();
$server = new Server('test');
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind("tcp://127.0.0.1:21002");
$pull->on('message', array($server, 'onNewRequest'));
$logger->info("ZMQ listening on TCP 127.0.0.1:21002");
$webSock = new React\Socket\Server($loop);
$webSock->listen(21001, '0.0.0.0');
$webServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$server
)
)
),
$webSock
);
$logger->info("ICU Server listening on 0.0.0.0:21001");
$loop->run();
The HTTP request itself doesn't fail. Response code is 200 and there are no errors in error log. Also when echoing through-out the script, I see the script gets executed fully but ZeroMQ won't send it to the Websocket server.
I checked with netstat
and ports 21001 and 21002 are running and listening. Also I added iptables
entries for both (though 21001 should be enough since ZeroMQ is only used on the local level but just in case).
This is a CentOS 7 server and I have libzmq installed along with the zmq pecl extension.
$ php -i | grep "zmq"
/etc/php.d/zmq.ini
zmq
libzmq version => 4.1.3
Have I missed something in my code or could this be a server configuration issue?
After many days of debugging, the issue fell to SELinux.
It seems that ZeroMQ connections get denied when SELinux is in enforcing
mode.
For anyone encountering such issue, you need to disable SELinux:
sudo setenforce 0