Class: EZMQ::Subscriber
Overview
Subscribe socket that listens for messages with an optional topic.
Instance Attribute Summary (collapse)
-
- (Object) action
Returns the value of attribute action.
Attributes inherited from Socket
#context, #decode, #encode, #socket
Instance Method Summary (collapse)
-
- (Publisher) initialize(**options)
constructor
Creates a new Subscriber socket.
-
- (void) listen {|message, topic| ... }
By default, waits for a message and prints it to STDOUT.
-
- (Object) receive(**options) {|message, topic| ... }
Receive a message from the socket.
-
- (Boolean) subscribe(topic)
Establishes a new message filter on the socket.
-
- (Boolean) unsubscribe(topic)
Removes a message filter (as set with subscribe) from the socket.
Methods inherited from Socket
Constructor Details
- (Publisher) initialize(**options)
The default behaviour is to output and messages received to STDOUT.
Creates a new Subscriber socket.
19 20 21 22 |
# File 'lib/ezmq/subscribe.rb', line 19 def initialize(**) super :connect, ZMQ::SUB, subscribe [:topic] if [:topic] end |
Instance Attribute Details
- (Object) action
Returns the value of attribute action
7 8 9 |
# File 'lib/ezmq/subscribe.rb', line 7 def action @action end |
Instance Method Details
- (void) listen {|message, topic| ... }
This method returns an undefined value.
By default, waits for a message and prints it to STDOUT.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ezmq/subscribe.rb', line 59 def listen loop do if block_given? yield(*receive) else , topic = receive puts "#{ topic } #{ }" end end end |
- (Object) receive(**options) {|message, topic| ... }
This method blocks until a message arrives.
Receive a message from the socket.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ezmq/subscribe.rb', line 37 def receive(**) = '' @socket.recv_string = .match(/^(?<topic>[^\ ]*)\ (?<body>.*)/) decoded = ([:decode] || @decode).call ['body'] if block_given? yield decoded, ['topic'] else [decoded, ['topic']] end end |
- (Boolean) subscribe(topic)
By default, a Subscriber filters all incoming messages. Without
Establishes a new message filter on the socket.
calling subscribe at least once, no messages will be accepted. If topic was provided, #initialize calls #subscribe automatically.
prefix will be accepted.
81 82 83 |
# File 'lib/ezmq/subscribe.rb', line 81 def subscribe(topic) @socket.setsockopt(ZMQ::SUBSCRIBE, topic) == 0 end |
- (Boolean) unsubscribe(topic)
Removes a message filter (as set with subscribe) from the socket.
92 93 94 |
# File 'lib/ezmq/subscribe.rb', line 92 def unsubscribe(topic) @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0 end |