I'm creating a chat on my website, a push notifications system, users activity widget ( updating on the fly ) etc.
My website is built on PHP, so I decided to use Ratchet as a websocket server for my tasks. I've installed all required components and I learned the guide on http://socketo.me/docs/push and started to code.
This is inside a ChatMsg( $item ){...}
model.php
PUSH
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($sData));
push-server.php
PULL
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->setSockOpt(ZMQ::SOCKOPT_HWM, 0);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('error', function ($e) {
$f = fopen('push-server-error.log', "a");
fwrite($f, $e->getMessage()."\n");
fclose($f);
});
$pull->on('message', array($pusher, 'onNewEvent'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8081, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
?>
push-server.php
PUSH
push-server.php
->on(){...}
push-server.php
->on(){...}
->send()
var_dump($socket->send(json_encode($sData)));
Server OS: CentOS 6.9 (Final)
PHP version: 5.6.31
ZMQ extension version: 1.1.3
libzmq version: 4.2.2
I created not persistent ZMQ::Context
and ZMQ::Socket
and my problem was solved:
$context = new ZMQContext(1, false);
$socket = $context->getSocket(ZMQ::SOCKET_PUSH);