i'm using faye websockets , redis attempt master/client websocket setup slideshow presentation. clients' should follow along master (i.e. master moves forward slide, message sent out of clients, , move forward slide).
the issue i'm having list of client websockets empty when access inside thread created in initialize function. separate thread necessary redis' 'subscribe' blocking function. file middleware , runs on app boot up.
i know point of global variables in rails they're shared across threads, in case seems preventing it.
is there way store list of websockets in globally accessible place? globally, in, running instances of app on same server. (can't use redis cause can't store objects).
require 'faye/websocket' require 'redis' class wscommunication keepalive_time = 15 #seconds channel = 'vip' def initialize(app) @app = app $clients = [] uri = uri.parse(env['rediscloud_url']) thread.new redis_sub = redis.new(host: uri.host, port: uri.port, password: uri.password) redis_sub.subscribe(channel) |on| on.message |channel, msg| puts 'client list on thread' puts $clients #### prints nothing $clients.each { |ws| ws.send(msg) } end end end end def call(env) if faye::websocket.websocket?(env) ws = faye::websocket.new(env, nil, {ping: keepalive_time}) ws.on :open |event| $clients << ws end ws.on :message |event| puts 'client list' puts $clients ### prints full list of clients $redis.publish(channel, event.data) end ws.on :close |event| $clients.delete(ws) ws = nil end # return async rack response ws.rack_response else @app.call(env) end end end
Comments
Post a Comment