Class Observer

An event observer for ØMQ sockets. This starts up a ZMQ monitoring socket internally that receives all socket events. The event observer can be used in one of two ways, which are mutually exclusive: with receive() or with event listeners attached with on().

Hierarchy (view full)

Constructors

Properties

Methods

Constructors

  • Creates a new ØMQ observer. It should not be necessary to instantiate a new observer. Access an existing observer for a socket with Socket.events.

    const socket = new Publisher()
    const events = socket.events

    Parameters

    • socket: Socket

      The socket to observe.

    Returns Observer

Properties

closed: boolean

Whether the observer was closed, either manually or because the associated socket was closed.

Methods

  • Asynchronously iterate over socket events. When the socket is closed or when the observer is closed manually with Observer.close(), the iterator will return.

    for await (event of socket.events) {
    switch (event.type) {
    case "bind":
    console.log(`Socket bound to ${event.address}`)
    break
    // ...
    }
    }

    Returns AsyncIterator<Event, undefined, undefined>

  • Closes the observer. Afterwards no new events will be received or emitted. Calling this method is optional.

    Returns void

  • Removes the specified listener function from the list of functions to call when the given event is observed.

    Type Parameters

    • E extends
          | "unknown"
          | "connect"
          | "disconnect"
          | "close"
          | "end"
          | "accept"
          | "accept:error"
          | "bind"
          | "bind:error"
          | "connect:delay"
          | "connect:retry"
          | "close:error"
          | "handshake"
          | "handshake:error:protocol"
          | "handshake:error:auth"
          | "handshake:error:other"

    Parameters

    • type: E

      The type of event that the listener was listening for.

    • listener: ((data: EventOfType<E>) => void)

      The previously registered listener function.

    Returns EventSubscriber

  • Adds a listener function which will be invoked when the given event type is observed. Calling this method will convert the Observer to event emitter mode, which will make it impossible to call Observer.receive() at the same time.

    socket.events.on("bind", event => {
    console.log(`Socket bound to ${event.address}`)
    // ...
    })

    Type Parameters

    • E extends
          | "unknown"
          | "connect"
          | "disconnect"
          | "close"
          | "end"
          | "accept"
          | "accept:error"
          | "bind"
          | "bind:error"
          | "connect:delay"
          | "connect:retry"
          | "close:error"
          | "handshake"
          | "handshake:error:protocol"
          | "handshake:error:auth"
          | "handshake:error:other"

    Parameters

    • type: E

      The type of event to listen for.

    • listener: ((data: EventOfType<E>) => void)

      The listener function that will be called with all event data when the event is observed.

    Returns EventSubscriber

  • Waits for the next event to become availeble on the observer. Reads an event immediately if possible. If no events are queued, it will wait asynchonously. The promise will be resolved with the next event when available.

    When reading events with receive() the observer may not be in event emitter mode. Avoid mixing calls to receive() with event handlers via attached with on().

    for await (event of socket.events) {
    switch (event.type) {
    case "bind":
    console.log(`Socket bound to ${event.address}`)
    break
    // ...
    }
    }

    Returns Promise<Event>

    Resolved with the next event and its details. See Event.