PHP using ZEROMQ

Source: Internet
Author: User
Tags zts

What the hell is a zeromq?

Connect your code in any language, on any platform.
carries messages across InProc, IPC, TCP, TPIC, multicast.
Smart patterns like Pub-sub, Push-pull, and Router-dealer.
high-speed asynchronous I/O engines, in a tiny library.
backed by a large and active open source community.
Supports every modern language and platform.
Build any architecture:centralized, distributed, small, or large.
Free software withFull commercial support


The above is an introduction to the official page, the main idea is that a cross-platform, can use any language using the message middleware, can be through inproc,ipc,tcp,tpic, multicast messaging messages, including a variety of modes, pub-sub (distribution-subscription), Push-pull (push mode), Router-dealer (route mode) and so on. Extremely high processing speed is one of its important characteristics.

For more information, please visit: http://zeromq.org/


Installation:

#wget http://download.zeromq.org/zeromq-4.0.4.tar.gz

#tar ZXVF zeromq-4.0.4.tar.gz

#cd zeromq-4.0.4

#./configure=/usr/local/zeromq404

#make

#make Install


To install the PHP extension:

#git Clone Git://github.com/mkoppanen/php-zmq.git

#cd PHP-ZMQ

#/usr/local/php/bin/phpize//own PHP installation directory, change as needed

#./configure--with-php-config=/usr/local/php/bin/php-config--with-zmq=/usr/local/zeromq404// Php-config needs to be changed according to its own situation

#make

#make Install


After installation, the zmq.so will be generated under the/usr/local/php/lib/php/extensions/no-debug-zts-20090626/directory.


To modify a configuration file:

#vim/etc/php.ini

Modify Extension_dir= "/usr/local/php/lib/php/extensions/no-debug-zts-20090626/"

Increase extension=zmq.so


Restart Apache

Visit Phpinfo, if you see the ZMQ information indicates that it is OK


Test:

The system is divided into two parts: client and server Side

Server side is generally performed by PHPCLI, resident background, listening to a port, this example uses 5555, the code is as follows:


zmqserver.php

<?php

/*

* * Hello World Server

* * Binds REP socket to tcp://*:5555

* * expects "Hello" from the client, replies with "World"

* * @author Ian Barber <ian (dot) Barber (at) gmail (dot) com>

* */


$context = new Zmqcontext (1);


Socket to talk to clients

$responder = new Zmqsocket ($context, Zmq::socket_rep);

$responder->bind ("tcp://*:5555");


while (true) {

$request = $responder->recv ();

printf ("Received Request: [%s]\n", $request);


Logtxt ($request);


Usleep (100);

$responder->send ("World");

}


function Logtxt ($msg) {

$handler = fopen ("/tmp/log/zmq.log", "A +");

Fwrite ($handler, date (' y-m-d h:i:s '). ' '. $msg. ' \ r \ n ");

Fclose ($handler);

}



When I do

#php zmqserver.php

When, error, can not find the "Zmqcontext" class


Use

#php-M

View loaded classes, find wood there, only some default


Continue to use

#php--ini

found that the path to the load INI is not/etc/php.ini


So Cp/etc/php.ini/usr/local/php/lib/php.ini

Execute again

#php zmqserver.php

Ok


Check to see if Port 5555 is being monitored:

#lsof-i:5555

appeared on my machine.

COMMAND PID USER FD TYPE DEVICE size/off NODE NAME

PHP 35145 root 9u IPv4 272314 0t0 TCP *:p ersonal-agent (LISTEN)


Indicates that the listener was successful.



Write the client side below


zmqclient.php

<?php

/*

* * Hello World Client

* * Connects REQ socket to tcp://localhost:5555

* * sends ' Hello ' to server, expects ' world ' back

* * @author Ian Barber <ian (dot) Barber (at) gmail (dot) com>

* */


$context = new Zmqcontext ();


Socket to talk to server

echo "Connecting to Hello World server...\n";

$requester = new Zmqsocket ($context, zmq::socket_req);

$requester->connect ("tcp://localhost:5555");

$date = Mktime ();



if ($requester->send ($date)!== false) {

echo "Send success\n";

}

$reply = $requester->recv ();

printf ("received:[%s]\n", $reply);



Access zmqclient.php from the browser

The world data can be received correctly, and there are newly generated log files under the/tmp/log/zmq.log

Show that everything is fine.


Ps:

This example uses a simple: Rep/req request response mode, in fact, ZMQ support the pattern is very many, the use of the scene is not the same, you can according to their own situation flexibly choose the appropriate mode.


Some useful resources:

Official website: http://zeromq.org/

http://blog.fity.cn/post/382/

http://iyuan.iteye.com/category/148998


PHP using ZEROMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.