<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Receives a message</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="zmqsocket.ispersistent.html">ZMQSocket::isPersistent</a></div> <div class="next" style="text-align: right; float: right;"><a href="zmqsocket.recvmulti.html">ZMQSocket::recvMulti</a></div> <div class="up"><a href="class.zmqsocket.html">ZMQSocket</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="zmqsocket.recv" class="refentry"> <div class="refnamediv"> <h1 class="refname">ZMQSocket::recv</h1> <p class="verinfo">(PECL zmq >= 0.5.0)</p><p class="refpurpose"><span class="refname">ZMQSocket::recv</span> — <span class="dc-title">Receives a message</span></p> </div> <div class="refsect1 description" id="refsect1-zmqsocket.recv-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><strong>ZMQSocket::recv</strong></span> ([ <span class="methodparam"><span class="type">integer</span> <code class="parameter">$mode</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Receive a message from a socket. By default receiving will block until a message is available unless <strong><code>ZMQ::MODE_NOBLOCK</code></strong> flag is used. <strong><code>ZMQ::SOCKOPT_RCVMORE</code></strong> socket option can be used for receiving multi-part messages. See <span class="function"><a href="zmqsocket.setsockopt.html" class="function">ZMQSocket::setSockOpt()</a></span> for more information. </p> </div> <div class="refsect1 parameters" id="refsect1-zmqsocket.recv-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">mode</code></em></span> <dd> <p class="para"> Pass mode flags to receive multipart messages or non-blocking operation. See <strong><code>ZMQ::MODE_*</code></strong> constants. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 examples" id="refsect1-zmqsocket.recv-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4698"> <p><strong>Example #1 A send/recv example</strong></p> <div class="example-contents"><p> Non-blocking send / receive </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* Create new queue object, there needs to be a server at the other end */<br /></span><span style="color: #0000BB">$queue </span><span style="color: #007700">= new </span><span style="color: #0000BB">ZMQSocket</span><span style="color: #007700">(new </span><span style="color: #0000BB">ZMQContext</span><span style="color: #007700">(), </span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">SOCKET_REQ</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$queue</span><span style="color: #007700">-></span><span style="color: #0000BB">connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://127.0.0.1:5555"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Assign socket 1 to the queue, send and receive */<br /></span><span style="color: #0000BB">$retries </span><span style="color: #007700">= </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sending </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Start a loop */<br /></span><span style="color: #007700">do {<br /> try {<br /> </span><span style="color: #FF8000">/* Try to send / receive */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$sending</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Sending message\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$queue</span><span style="color: #007700">-></span><span style="color: #0000BB">send</span><span style="color: #007700">(</span><span style="color: #DD0000">"This is a message"</span><span style="color: #007700">, </span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">MODE_NOBLOCK</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$sending </span><span style="color: #007700">= </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /> } else {<br /> echo </span><span style="color: #DD0000">"Got response: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$queue</span><span style="color: #007700">-></span><span style="color: #0000BB">recv</span><span style="color: #007700">(</span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">MODE_NOBLOCK</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> break;<br /> }<br /> } catch (</span><span style="color: #0000BB">ZMQSocketException $e</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* EAGAIN means that the operation would have blocked, retry */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getCode</span><span style="color: #007700">() === </span><span style="color: #0000BB">ZMQ</span><span style="color: #007700">::</span><span style="color: #0000BB">ERR_EAGAIN</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">" - Got EAGAIN, retrying (</span><span style="color: #0000BB">$retries</span><span style="color: #DD0000">)\n"</span><span style="color: #007700">;<br /> } else {<br /> die(</span><span style="color: #DD0000">" - Error: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br /> }<br /> }<br /> </span><span style="color: #FF8000">/* Sleep a bit between operations */<br /> </span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br />} while (--</span><span style="color: #0000BB">$retries</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Sending message - Unable to execute operation, retrying (4) Got response: This is a message </pre></div> </div> </div> </p> </div> <div class="refsect1 returnvalues" id="refsect1-zmqsocket.recv-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the message. Throws ZMQSocketException in error. If <strong><code>ZMQ::MODE_NOBLOCK</code></strong> is used and the operation would block <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> false shall be returned. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="zmqsocket.ispersistent.html">ZMQSocket::isPersistent</a></div> <div class="next" style="text-align: right; float: right;"><a href="zmqsocket.recvmulti.html">ZMQSocket::recvMulti</a></div> <div class="up"><a href="class.zmqsocket.html">ZMQSocket</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>