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.



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

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

Instance Method Details

#closeObject



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

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



45
46
47
48
49
# File 'lib/async/redis/context/subscribe.rb', line 45

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

#subscribe(channels) ⇒ Object



51
52
53
54
# File 'lib/async/redis/context/subscribe.rb', line 51

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

#unsubscribe(channels) ⇒ Object



56
57
58
59
# File 'lib/async/redis/context/subscribe.rb', line 56

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