Search code examples
phpzeromqratchet

SOCKET_PUSH not sending anything using ZMQContext? [Blocked by FireWall ...]


I am trying to follow this tutorial for Ratchet/ZMQ Socket programming: http://socketo.me/docs/push

with a bit of customisation to learn abit more about it.

The server itself is running fine and the connection between front-end html seems to be connecting right. But I cant figure the PHP file that sends a message to the server.

CODES BELOW:

SENDER.PHP

$context    = new ZMQContext();
$socket     = $context->getSocket(ZMQ::SOCKET_PUSH,'my pusher');
$socket->connect('tcp://127.0.0.1:5555');
$socket->send("SENDING A MESSAGE");

The code above is what Im having problems with. When I run the code in the command line

php sender.php

The server should at least display some feed back but it doesnt give me anything. And the sender.php just exits. Im stuck trying to figure out what Im missing. At least the front-html bit works. How can I get the sender.php to send the message? Any suggestion/advise/help would be greatly appreciated.

Below are the rest of my codes:

INDEX.html

This html file is connecting as Im getting the message from the constructor.

ab.debug(true,true);
var conn = new ab.Session('ws://localhost:8080',
        function() {
            conn.subscribe('kittensCategory', function(data) {
                // This is where you would add the new article to the DOM (beyond the scope of this tutorial)
                console.log("New data available: ",data);
            });
        },
        function() {
            console.warn('WebSocket connection closed');
        },
        {'skipSubprotocolCheck': true}
);

SERVER.PHP

use Models\SCSTRealtimeSubsObject;

// The event loop that will keep on triggering
$loop   = React\EventLoop\Factory::create();

// Our custom pusher that will do the logic, $loop is optional
$pusher =  new SCSTRealtimeSubsObject;

// 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);
//Binding to itself means the client can only connect to itself
$pull->bind("tcp://127.0.0.1:5555");
//On a 'message' event, pass the data to the myMessageHandler method of the MyPusherClass
$pull->on('message', array($pusher, 'customAction'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // 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();

PUSHER.PHP

class SCSTRealtimeSubsObject implements WampServerInterface {

    public function __construct() {
        echo "Constructor call. \n";
    }
    public function customAction($msg){ // Message from the onMessage
        echo "There was a message: $msg";
    }
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    //                  WampServerInterface Implementations
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    public function onOpen(ConnectionInterface $conn) {
        echo "New connection! ({$conn->resourceId}) \n";
    }
    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        echo "Connection {$conn->resourceId} has disconnected \n";
    }
    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "There is an error ". $e->getMessage();
    }
    public function onSubscribe(ConnectionInterface $conn, $topic) {
        echo "New subscriber : $topic \n";
    }
    public function onUnSubscribe(ConnectionInterface $conn, $topic) {
        echo "Unsubscribed : $topic \n";
    }
    public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
        // In this application if clients send data it's because the user hacked around in console
        $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    }
    public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
        // In this application if clients send data it's because the user hacked around in console
        echo "Published $topic. \n";
        $conn->close();
    }
}

Solution

  • Finally found the answer. I forgot to enclosed that my development server is on Windows.

    In my case, php cli is firewalled by windows and is blocked from accessing any networks and ports.

    To fix this go to control panel -> windows firewall. Look for inbound CLI this is most likely the PhP exe. Allow access to network and that should do the trick.