Sha256: 20e8bb84b72f3a08d8597ddece259cd944105c8a8971b225866ed6054919576e

Contents?: true

Size: 1010 Bytes

Versions: 2

Compression:

Stored size: 1010 Bytes

Contents

require_relative 'socket'

# Syntactic sugar for 0MQ, because Ruby shouldn't feel like C.
module EZMQ
  # Publish socket that broadcasts messages with an optional topic.
  class Publisher < EZMQ::Socket
    # Creates a new Publisher socket.
    #
    # @param [Hash] options optional parameters.
    # @see EZMQ::Socket EZMQ::Socket for optional parameters.
    #
    # @return [Publisher] a new instance of Publisher.
    #
    def initialize(**options)
      super :bind, ZMQ::PUB, options
    end

    # Sends a message on the socket, with an optional topic.
    #
    # @param [String] message the message to send.
    # @param [String] topic an optional topic for the message.
    # @param [Hash] options optional parameters.
    # @option options [lambda] encode how to encode the message.
    #
    # @return [Fixnum] the size of the message.
    #
    def send(message = '', topic: '', **options)
      @socket.send_string "#{ topic } #{ (options[:encode] || @encode).call message }"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ezmq-0.3.2 lib/ezmq/publish.rb
ezmq-0.3.1 lib/ezmq/publish.rb