Interface Readable<M>

Describes sockets that can receive messages.

M The type of the message or message parts that can be read.

interface Readable<M> {
    receiveBufferSize: number;
    receiveHighWaterMark: number;
    receiveTimeout: number;
    [asyncIterator](): AsyncIterator<M, undefined, undefined>;
    receive(): Promise<M>;
}

Type Parameters

Hierarchy (view full)

Properties

receiveBufferSize: number

ZMQ_RCVBUF

Underlying kernel receive buffer size in bytes. A value of -1 means leave the OS default unchanged.

receiveHighWaterMark: number

ZMQ_RCVHWM

The high water mark is a hard limit on the maximum number of incoming messages ØMQ shall queue in memory for any single peer that the specified socket is communicating with. A value of zero means no limit.

If this limit has been reached the socket shall enter an exceptional state and depending on the socket type, ØMQ shall take appropriate action such as blocking or dropping sent messages.

receiveTimeout: number

ZMQ_RCVTIMEO

Sets the timeout receiving messages on the socket. If the value is 0, receive() will return a rejected promise immediately if there is no message to receive. If the value is -1, it will wait asynchronously until a message is available. For all other values, it will wait for a message for that amount of time before rejecting.

Methods

  • Asynchronously iterate over messages becoming available on the socket. When the socket is closed with Socket.close(), the iterator will return. Returning early from the iterator will not close the socket unless it also goes out of scope.

    for await (const [msg] of socket) {
    // handle messages
    }

    Returns AsyncIterator<M, undefined, undefined>

  • Waits for the next single or multipart message to become availeble on the socket. Reads a message immediately if possible. If no messages can be read, it will wait asynchonously. The promise will be resolved with an array containing the parts of the next message when available.

    const [msg] = await socket.receive()
    const [part1, part2] = await socket.receive()

    Reading may fail (eventually) if the socket has been configured with a receiveTimeout.

    A call to receive() is guaranteed to return with a resolved promise immediately if a message could be read from the socket directly.

    Only one asynchronously blocking call to receive() can be in progress simultaneously. If you call receive() again on the same socket it will return a rejected promise with an EBUSY error. For example, if no messages can be read and no await is used:

    socket.receive() // -> pending promise until read is possible
    socket.receive() // -> promise rejection with `EBUSY` error

    Note: Due to the nature of Node.js and to avoid blocking the main thread, this method always attempts to read messages with the ZMQ_DONTWAIT flag. It polls asynchronously if reading is not currently possible. This means that all functionality related to timeouts and blocking behaviour is reimplemented in the Node.js bindings. Any differences in behaviour with the native ZMQ library is considered a bug.

    Returns Promise<M>

    Resolved with message parts that were successfully read.