Using ZeroMQ in a PHP script inside Apache
Question: I want to use the ZeroMQ publisher/subscriber to dispatch data from my web
application to multiple servers.

I use Apache and PHP for the web app, my php script works as follow:

    
    
    //Initialization
    $context = new ZMQContext();
    $publisher = $context->getSocket(ZMQ::SOCKET_PUB);
    $publisher->bind("tcp://*:5556");
    
    //Then publishing for testing:
    
    $publisher->send("test");
    $publisher->send("test");
    $publisher->send("test");
    $publisher->send("test");
    $publisher->send("test");
    

For testing I adapted a subscriber from the documentation in python:

    
    
    import sys
    import zmq
    
    #  Socket to talk to server
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    socket.connect ("tcp://localhost:5556")
    
    # Subscribe to zipcode, default is NYC, 10001
    socket.setsockopt(zmq.SUBSCRIBE, "")
    
    print "Waiting..."
    # Process 5 updates
    for update_nbr in range (5):
        string = socket.recv()
        print string
    

The whole thing works when I run the php script from command line but does not
work through Apache (when the script is run through a web browser).

Is there anything I should do to my Apache configuration to make it work ?

Thanks

Alexandre


Answer: It seems that the only problem was that the connection didn't have time to be
established.

Adding a sleep on the publisher after the binding and before the send did
resolve the issue, even though not quite elegantly.

The issue is explained here:

<http://zguide.zeromq.org/page:all#Getting-the-Message-Out>

