Skip to content

Persistent context socket

mkoppanen edited this page Sep 14, 2010 · 6 revisions

The ini-setting ‘zeromq.persist_context’ defines whether the zmq context will be persisted over requests and must be set ‘On’ for persistent connections to work. Each combination of application/io threads creates a new context which is then persisted and re-used assuming ‘zeromq.persist_context’ is set ‘On’.

ZeroMQ constructor takes optional second parameter for the constructor called ‘persistent_id’. This value is used as a key for persisting socket and retrieving over multiple requests. The persistent socket must be allocated using a persistent context and will fall back being non-persistent if ‘zeromq.persist_context’ is set ‘Off’.

Sockets are stored using persistent list key ‘zeromq_socket:[%d]-[%s]’ where %d is the socket type and %s is the persistent_id. Therefore the following code:


$s1 = new ZeroMQ(ZeroMQ::SOCKET_P2P, 'myappsocket');
$s2 = new ZeroMQ(ZeroMQ::SOCKET_REP, 'myappsocket');

would create two separate sockets. Only sockets with same type and persistent id are shared between different objects.

Note: It’s important to remember that careless use of persistent sockets can exhaust available file-handles on the machine.

Clone this wiki locally