Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created November 15, 2010 02:30
Show Gist options
  • Save dongyuwei/676345 to your computer and use it in GitHub Desktop.
Save dongyuwei/676345 to your computer and use it in GitHub Desktop.
redis pub sub test
require "rubygems"
require "redis"
puts <<-EOS
To play with this example use redis-cli from another terminal, like this:
$ redis-cli publish test msg
EOS
redis = Redis.connect
trap(:INT) { puts; exit }
redis.subscribe(:test) do |on|
on.message do |channel, message|
puts "##{channel}: #{message}"
#必须建立一个新连接,否则必报错。
#lib/redis/connection.rb:86:in `format_error_reply': -
#ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context (RuntimeError)
redis2 = Redis.connect
puts redis2.get 'dyw'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment