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 { 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 { m }, **options)
Creates a new Subscriber socket.
185 186 187 188 189 |
# File 'lib/ezmq.rb', line 185 def initialize(action: -> m { 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.
223 224 225 |
# File 'lib/ezmq.rb', line 223 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.
202 203 204 |
# File 'lib/ezmq.rb', line 202 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.
213 214 215 |
# File 'lib/ezmq.rb', line 213 def unsubscribe(topic) @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0 ? true : false end |