Last active
December 10, 2015 09:58
-
-
Save the-architect/4417375 to your computer and use it in GitHub Desktop.
Redis Pubsub test with backlog of posted messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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