Skip to content

Instantly share code, notes, and snippets.

@ashayh
Forked from ekorneeff/faye.ru
Created October 24, 2012 17:34
Show Gist options
  • Save ashayh/3947527 to your computer and use it in GitHub Desktop.
Save ashayh/3947527 to your computer and use it in GitHub Desktop.
Faye online/offline status to Rails via Redis
# Run with: rackup faye.ru -s thin -E production
# For executor-customer chat
require "bundler/setup"
require "faye"
require 'redis'
require 'redis/objects'
Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)
Faye::WebSocket.load_adapter('thin')
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
# some code for logging
faye_server.bind(:publish) do |client_id, channel, data|
puts "#{client_id}: #{data}. #{channel}"
end
# and that's here
faye_server.bind(:subscribe) do |client_id, channel|
puts "#{client_id} connected to #{channel}"
if /\/*\/messages\/*/.match(channel)
m = /\/sync\/messages\/(?<request_id>\d+)\/(?<who>\w+)/.match(channel)
par = m[:who] == "executor" ? "customer_online" : "executor_online"
Redis.current.set("request:#{m[:request_id]}:#{par}", "true")
end
end
faye_server.bind(:unsubscribe) do |client_id, channel|
puts "#{client_id} disconnected from #{channel}"
if /\/*\/messages\/*/.match(channel)
m = /\/sync\/messages\/(?<request_id>\d+)\/(?<who>\w+)/.match(channel)
par = m[:who] == "executor" ? "customer_online" : "executor_online"
Redis.current.set("request:#{m[:request_id]}:#{par}", "false")
end
end
run faye_server.listen(9292)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment