class ZMQSocket (View source)

Class ZMQSocket

Methods

__construct(ZMQContext $context, int $type, string $persistent_id = null, callable $on_new_socket = null)

(PECL zmq >= 0.5.0) Constructs a ZMQSocket object.

bind(string $dsn, bool $force = false)

(PECL zmq >= 0.5.0) Bind the socket to an endpoint.

connect(string $dsn, bool $force = false)

(PECL zmq >= 0.5.0) Connect the socket to a remote endpoint.

disconnect(string $dsn)

(PECL zmq >= 1.0.4) Disconnect the socket from a previously connected remote endpoint.

array
getEndpoints()

Returns a list of endpoints where the socket is connected or bound to.

string|null
getPersistentId()

Returns the persistent id string assigned of the object and NULL if socket is not persistent.

string|int
getSockOpt(int $key)

Returns the value of a socket option.

int
getSocketType()

Return the socket type.

bool
isPersistent()

Check whether the socket is persistent.

string|false
recv(int $mode = 0)

Receive a message from a socket.

string[]
recvMulti(int $mode = 0)

Receive an array multipart message from a socket.

send(string $message, int $mode = 0)

Send a message using the socket. The operation can block unless ZMQ::MODE_NOBLOCK is used.

sendmulti(array $message, int $mode = 0)

Send a multipart message using the socket. The operation can block unless ZMQ::MODE_NOBLOCK is used.

setSockOpt(int $key, mixed $value)

Sets a ZMQ socket option. The type of the value depends on the key.

unbind(string $dsn)

Unbind the socket from an endpoint.

Details

__construct(ZMQContext $context, int $type, string $persistent_id = null, callable $on_new_socket = null)

(PECL zmq >= 0.5.0) Constructs a ZMQSocket object.

The persistent_id parameter can be used to allocated a persistent socket. A persistent socket has to be allocated from a persistent context and it stays connected over multiple requests. The persistent_id parameter can be used to recall the same socket over multiple requests. The on_new_socket is called only when a new underlying socket structure is created.

Parameters

ZMQContext $context

ZMQContext to build this object

int $type

The type of the socket. See ZMQ::SOCKET_* constants.

string $persistent_id

[optional]

If persistent_id is specified the socket will be persisted over multiple requests. If context is not persistent the socket falls back to non-persistent mode.

callable $on_new_socket

[optional]

Callback function, which is executed when a new socket structure is created. This function does not get invoked if the underlying persistent connection is re-used.

Exceptions

ZMQSocketException

ZMQSocket bind(string $dsn, bool $force = false)

(PECL zmq >= 0.5.0) Bind the socket to an endpoint.

The endpoint is defined in format transport://address where transport is one of the following: inproc, ipc, tcp, pgm or epgm.

Parameters

string $dsn

The bind dsn, for example transport://address.

bool $force

Tries to bind even if the socket has already been bound to the given endpoint.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

ZMQSocket connect(string $dsn, bool $force = false)

(PECL zmq >= 0.5.0) Connect the socket to a remote endpoint.

The endpoint is defined in format transport://address where transport is one of the following: inproc, ipc, tcp, pgm or epgm.

Parameters

string $dsn

The bind dsn, for example transport://address.

bool $force

Tries to bind even if the socket has already been bound to the given endpoint.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

ZMQSocket disconnect(string $dsn)

(PECL zmq >= 1.0.4) Disconnect the socket from a previously connected remote endpoint.

The endpoint is defined in format transport://address where transport is one of the following: inproc, ipc, tcp, pgm or epgm.

Parameters

string $dsn

The bind dsn, for example transport://address.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

array getEndpoints()

Returns a list of endpoints where the socket is connected or bound to.

Return Value

array

contains two sub-arrays: 'connect' and 'bind'

Exceptions

ZMQSocketException

string|null getPersistentId()

Returns the persistent id string assigned of the object and NULL if socket is not persistent.

Return Value

string|null

Returns the persistent id string assigned of the object and NULL if socket is not persistent.

string|int getSockOpt(int $key)

Since: 0MQ 2.0.7

Returns the value of a socket option.

This method is available if ZMQ extension has been compiled against ZMQ version 2.0.7 or higher

Parameters

int $key

An int representing the option. See the ZMQ::SOCKOPT_* constants.

Return Value

string|int

Returns either a string or an integer depending on key. Throws ZMQSocketException on error.

Exceptions

ZMQSocketException

int getSocketType()

Return the socket type.

The socket type can be compared against ZMQ::SOCKET_* constants.

Return Value

int

Returns an integer representing the socket type. The integer can be compared against ZMQ::SOCKET_* constants.

bool isPersistent()

Check whether the socket is persistent.

Return Value

bool

Returns a boolean based on whether the socket is persistent or not.

string|false recv(int $mode = 0)

Receive a message from a socket.

By default receiving will block until a message is available unless ZMQ::MODE_NOBLOCK flag is used. ZMQ::SOCKOPT_RCVMORE socket option can be used for receiving multi-part messages. Returns the message. If ZMQ::MODE_NOBLOCK is used and the operation would block bool false shall be returned.

Parameters

int $mode

Pass mode flags to receive multipart messages or non-blocking operation. See ZMQ::MODE_* constants.

Return Value

string|false

Returns the message. Throws ZMQSocketException in error. If ZMQ::MODE_NOBLOCK is used and the operation would block boolean false shall be returned.

Exceptions

ZMQSocketException

See also

ZMQSocket::setSockOpt

string[] recvMulti(int $mode = 0)

Receive an array multipart message from a socket.

By default receiving will block until a message is available unless ZMQ::MODE_NOBLOCK flag is used. Returns the array of message parts. If ZMQ::MODE_NOBLOCK is used and the operation would block bool false shall be returned.

Parameters

int $mode

Pass mode flags to receive multipart messages or non-blocking operation. See ZMQ::MODE_* constants.

Return Value

string[]

Returns the array of message parts. Throws ZMQSocketException in error. If ZMQ::MODE_NOBLOCK is used and the operation would block boolean false shall be returned.

Exceptions

ZMQSocketException

ZMQSocket send(string $message, int $mode = 0)

Send a message using the socket. The operation can block unless ZMQ::MODE_NOBLOCK is used.

If ZMQ::MODE_NOBLOCK is used and the operation would block bool false shall be returned.

Parameters

string $message

The message to send

int $mode

Pass mode flags to receive multipart messages or non-blocking operation. See ZMQ::MODE_ constants.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

ZMQSocket sendmulti(array $message, int $mode = 0)

Send a multipart message using the socket. The operation can block unless ZMQ::MODE_NOBLOCK is used.

If ZMQ::MODE_NOBLOCK is used and the operation would block bool false shall be returned.

Parameters

array $message

The message to send - an array of strings

int $mode

Pass mode flags to receive multipart messages or non-blocking operation. See ZMQ::MODE_ constants.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

ZMQSocket setSockOpt(int $key, mixed $value)

Sets a ZMQ socket option. The type of the value depends on the key.

Parameters

int $key

One of the ZMQ::SOCKOPT_* constants.

mixed $value

The value of the parameter.

Return Value

ZMQSocket

Exceptions

ZMQSocketException

See also

ZMQ Constant Types for more information.

ZMQSocket unbind(string $dsn)

Unbind the socket from an endpoint.

The endpoint is defined in format transport://address where transport is one of the following: inproc, ipc, tcp, pgm or epgm.

Parameters

string $dsn

The previously bound dsn, for example transport://address.

Return Value

ZMQSocket

Exceptions

ZMQSocketException