Class: Async::Redis::Context::Subscribe

Inherits:
Generic
  • Object
show all
Defined in:
lib/async/redis/context/subscribe.rb

Constant Summary collapse

MESSAGE =
'message'

Instance Method Summary collapse

Methods inherited from Generic

#call, #read_response, #write_request

Constructor Details

#initialize(pool, channels) ⇒ Subscribe

Returns a new instance of Subscribe.



15
16
17
18
19
# File 'lib/async/redis/context/subscribe.rb', line 15

def initialize(pool, channels)
	super(pool)
	
	subscribe(channels)
end

Instance Method Details

#closeObject



21
22
23
24
25
26
# File 'lib/async/redis/context/subscribe.rb', line 21

def close
	# There is no way to reset subscription state. On Redis v6+ you can use RESET, but this is not supported in <= v6.
	@connection&.close
	
	super
end

#listenObject



28
29
30
31
32
# File 'lib/async/redis/context/subscribe.rb', line 28

def listen
	while response = @connection.read_response
		return response if response.first == MESSAGE
	end
end

#subscribe(channels) ⇒ Object



34
35
36
37
# File 'lib/async/redis/context/subscribe.rb', line 34

def subscribe(channels)
	@connection.write_request ['SUBSCRIBE', *channels]
	@connection.flush
end

#unsubscribe(channels) ⇒ Object



39
40
41
42
# File 'lib/async/redis/context/subscribe.rb', line 39

def unsubscribe(channels)
	@connection.write_request ['UNSUBSCRIBE', *channels]
	@connection.flush
end