Skip to content

Instantly share code, notes, and snippets.

@the-architect
Last active December 10, 2015 09:58
Show Gist options
  • Save the-architect/4417375 to your computer and use it in GitHub Desktop.
Save the-architect/4417375 to your computer and use it in GitHub Desktop.
Redis Pubsub test with backlog of posted messages.
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
source 'https://rubygems.org'
gem 'redis'
gem "hiredis"
gem "em-synchrony"
gem 'json'
require 'bundler/setup'
require 'redis'
require 'json'
redis = Redis.new
channel = 'redis-pubsub-chat'
log = channel+'-log'
loop do
msg = STDIN.gets
if msg.strip != ""
msg = msg.strip
redis.publish channel, msg
now = Time.now.utc
timestamp = "#{now.to_i}#{now.nsec}"
redis.zadd log, timestamp, msg
end
end
require 'bundler/setup'
require 'redis'
require 'json'
redis = Redis.new(timeout: 0)
channel = 'redis-pubsub-chat'
log = channel+'-log'
old_data = redis.zrevrange(log, 0, -1)
old_data.each do |msg|
puts msg
end
redis.subscribe(channel) do |on|
on.message do |ch, msg|
puts msg
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment