zmq/connections

Search:
Group by:

Types

ZConnectionImpl {.pure, final.} = object
  context*: ZContext         ## Zmq context from C-bindings. 
  socket*: ZSocket           ## Zmq socket from C-bindings.
  
A Zmq connection. Since ZContext and ZSocket are pointers, it is highly recommended to not copy ZConnection.
ZmqError = object of IOError
  error*: cint
exception that is raised if something fails errno value code

Procs

proc `=copy`(dest: var ZConnectionImpl; source: ZConnectionImpl) {....raises: [],
    tags: [], forbids: [].}
proc `=sink`(dest: var ZConnectionImpl; source: ZConnectionImpl) {....raises: [],
    tags: [], forbids: [].}
proc bindAddr(conn: var ZConnection; address: string) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Bind the socket to a new address The socket must disconnected / unbind beforehand
proc close(c: ZConnection; linger: int = 500) {....raises: [ValueError, ZmqError],
    tags: [], forbids: [].}
proc connect(address: string; mode: ZSocketType): ZConnection {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Open a new connection on an internal (owned) ZContext and connects the socket

Example:

import zmq
var pull_conn = connect("tcp://127.0.0.1:34444", PULL)
var push_conn = listen("tcp://127.0.0.1:34444", PUSH)

let msgpayload = "hello world !"
push_conn.send(msgpayload)
assert pull_conn.receive() == msgpayload

push_conn.close()
pull_conn.close()
proc connect(address: string; mode: ZSocketType; context: ZContext): ZConnection {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Open a new connection on an external ZContext and connect the socket. External context are useful for inproc connections.
proc disconnect(conn: ZConnection) {....raises: [ValueError, ZmqError], tags: [],
                                     forbids: [].}
Disconnect the socket
proc getsockopt[T: SomeOrdinal | string](c: ZConnection; option: ZSockOptions): T

getsockopt on ZConnection

Careful, the sizeof of optval depends on the ZSockOptions passed. Check http://api.zeromq.org/4-2:zmq-setsockopt

proc getsockopt[T: SomeOrdinal | string](s: ZSocket; option: ZSockOptions): T

getsockopt on ZSocket

Careful, the sizeof of optval depends on the ZSockOptions passed. Check http://api.zeromq.org/4-2:zmq-setsockopt

proc listen(address: string; mode: ZSocketType): ZConnection {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Open a new connection on an internal (owned) ZContext and binds the socket
proc listen(address: string; mode: ZSocketType; context: ZContext): ZConnection {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Open a new connection on an external ZContext and binds on the socket. External context are useful for inproc connections.

Example:

import zmq
var monoserver = listen("tcp://127.0.0.1:34444", PAIR)
var monoclient = connect("tcp://127.0.0.1:34444", PAIR)

monoclient.send("ping")
assert monoserver.receive() == "ping"
monoserver.send("pong")
assert monoclient.receive() == "pong"

monoclient.close()
monoserver.close()
proc newZContext(): ZContext {....raises: [], tags: [], forbids: [].}
Create a new ZContext
proc newZContext(numthreads: int): ZContext {....raises: [ValueError, ZmqError],
    tags: [], forbids: [].}
Create a new ZContext with a thread pool set to numthreads
proc newZContext(option: int; optval: int): ZContext {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Create a new ZContext and set its options
proc proxy(frontend, backend, capture: ZConnection) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Same as proxy(frontend, backend: ZConnection) but enable the use of a capture socket. The proxy shall send all messages, received on both frontend and backend, to the capture socket. The capture socket should be a ZMQ_PUB, ZMQ_DEALER, ZMQ_PUSH, or ZMQ_PAIR socket.
proc proxy(frontend, backend: ZConnection) {....raises: [ValueError, ZmqError],
    tags: [], forbids: [].}
The proxy connects a frontend socket to a backend socket. Data flows from frontend to backend. Depending on the socket types, replies may flow in the opposite direction. Before calling proxy(), you must set any socket options, and connect or bind both frontend and backend sockets. The two conventional proxy models are: proxy() runs in the current thread and returns only if/when the current context is closed.
proc receive(c: ZConnection; flags: ZSendRecvOptions = NOFLAGS): string {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Receive data over the connection
proc receive(s: ZSocket; flags: ZSendRecvOptions = NOFLAGS): string {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Receive a message on socket.
proc receiveAll(c: ZConnection; flags: ZSendRecvOptions = NOFLAGS): seq[string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Receive all parts of a message

If EAGAIN occurs without any data being received, it will be an empty seq

proc receiveAll(s: ZSocket; flags: ZSendRecvOptions = NOFLAGS): seq[string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Receive all parts of a message

If EAGAIN occurs without any data being received, it will be an empty seq

proc reconnect(conn: var ZConnection; address: string) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Reconnect a socket to a new address
proc reconnect(conn: ZConnection) {....raises: [ValueError, ZmqError], tags: [],
                                    forbids: [].}
Reconnect a previously binded/connected address
proc send(c: ZConnection; msg: string; flags: ZSendRecvOptions = NOFLAGS) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Sends a message over the connection.
proc send(s: ZSocket; msg: string; flags: ZSendRecvOptions = NOFLAGS) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Sends a message through the socket.
proc sendAll(c: ZConnection; msg: varargs[string]) {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}
Send msg as a multipart message over the connection
proc sendAll(s: ZSocket; msg: varargs[string]) {....raises: [ValueError, ZmqError],
    tags: [], forbids: [].}
Send msg as a multipart message
proc setsockopt[T: SomeOrdinal | string](c: ZConnection; option: ZSockOptions;
    optval: T)

setsockopt on ZConnection

Careful, the sizeof of optval depends on the ZSockOptions passed. Check http://api.zeromq.org/4-2:zmq-setsockopt

proc setsockopt[T: SomeOrdinal | string](s: ZSocket; option: ZSockOptions;
    optval: T)

setsockopt on ZSocket

Careful, the sizeof of optval depends on the ZSockOptions passed. Check http://api.zeromq.org/4-2:zmq-setsockopt

proc terminate(ctx: ZContext) {....raises: [ValueError, ZmqError], tags: [],
                                forbids: [].}
Terminate the ZContext
proc tryReceive(c: ZConnection; flags: ZSendRecvOptions = NOFLAGS): tuple[
    msgAvailable: bool, moreAvailable: bool, msg: string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Receives a message from a socket in a non-blocking way.

Indicate whether a message was received or EAGAIN occured by msgAvailable

Indicate if more parts are needed to be received by moreAvailable

proc tryReceive(s: ZSocket; flags: ZSendRecvOptions = NOFLAGS): tuple[
    msgAvailable: bool, moreAvailable: bool, msg: string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Receives a message from a socket in a non-blocking way.

Indicate whether a message was received or EAGAIN occured by msgAvailable

Indicate if more parts are needed to be received by moreAvailable

proc unbind(conn: ZConnection) {....raises: [ValueError, ZmqError], tags: [],
                                 forbids: [].}
Unbind the socket
proc waitForReceive(c: ZConnection; timeout: int = -1;
                    flags: ZSendRecvOptions = NOFLAGS): tuple[
    msgAvailable: bool, moreAvailable: bool, msg: string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Set RCVTIMEO for the socket and wait until a message is available. This function is blocking.

timeout:

-1 means infinite wait

positive value is in milliseconds negative value strictly below -1 are ignored and the wait time will default to RCVTIMEO set for the socket (which by default is -1).

Indicate whether a message was received or EAGAIN occured by msgAvailable Indicate if more parts are needed to be received by moreAvailable

proc waitForReceive(s: ZSocket; timeout: int = -2;
                    flags: ZSendRecvOptions = NOFLAGS): tuple[
    msgAvailable: bool, moreAvailable: bool, msg: string] {.
    ...raises: [ValueError, ZmqError], tags: [], forbids: [].}

Set RCVTIMEO for the socket and wait until a message is available. This function is blocking.

timeout:

-1 means infinite wait

positive value is in milliseconds negative value strictly below -1 are ignored and the wait time will default to RCVTIMEO set for the socket (which by default is -1).

Indicate whether a message was received or EAGAIN occured by msgAvailable Indicate if more parts are needed to be received by moreAvailable

proc zmqError() {.noinline, noreturn, ...raises: [ValueError, ZmqError], tags: [],
                  forbids: [].}
raises ZmqError with error message from zmq.strerror.