Sha256: 51538391edeac3cb3c5c293e13aa4ec0d7d91b8247a61c54fa161fba79c5c910

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

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 [:bind, :connect] mode (:bind) a mode for the socket.
    # @param [Hash] options optional parameters.
    # @see EZMQ::Socket EZMQ::Socket for optional parameters.
    #
    # @return [Publisher] a new instance of Publisher.
    #
    def initialize(mode = :bind, **options)
      super mode, 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)
      message = "#{ topic } #{ (options[:encode] || @encode).call message }"
      @socket.send_string message
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ezmq-0.4.12 lib/ezmq/publish.rb
ezmq-0.4.11 lib/ezmq/publish.rb
ezmq-0.4.4 lib/ezmq/publish.rb