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(action: -> m { puts m }, **options)
constructor
Creates a new Subscriber socket.
-
- (void) listen(handler: -> { @action.call(receive) })
By default, waits for a message and calls @action with the message.
-
- (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
#bind, #connect, #receive, #send
Constructor Details
- (Publisher) initialize(action: -> m { puts m }, **options)
The default behaviour is to output and messages received to STDOUT.
Creates a new Subscriber socket.
187 188 189 190 191 |
# File 'lib/ezmq.rb', line 187 def initialize(action: -> m { puts m }, **) @action = action super :connect, ZMQ::SUB, subscribe [:topic] if [:topic] end |
Instance Attribute Details
- (Object) action
Returns the value of attribute action
172 173 174 |
# File 'lib/ezmq.rb', line 172 def action @action end |
Instance Method Details
- (void) listen(handler: -> { @action.call(receive) })
This method returns an undefined value.
By default, waits for a message and calls @action with the message.
225 226 227 |
# File 'lib/ezmq.rb', line 225 def listen(handler: -> { @action.call(receive) }) loop { handler.call } 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.
204 205 206 |
# File 'lib/ezmq.rb', line 204 def subscribe(topic) @socket.setsockopt(ZMQ::SUBSCRIBE, topic) == 0 ? true : false end |
- (Boolean) unsubscribe(topic)
Removes a message filter (as set with subscribe) from the socket.
215 216 217 |
# File 'lib/ezmq.rb', line 215 def unsubscribe(topic) @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0 ? true : false end |