简体   繁体   中英

Ruby Redis with em-synchrony and PubSub problems

I'm using the Cramp framework together with the Redis gem and em-synchrony and WebSockets to build a chat related web application, and having some problems.

consider the following code:

class DrawingAction < Cramp::Action
  use_fiber_pool

  self.transport = :websocket

  on_start :user_joined
  on_data :message_received
  on_finish :user_left

  def user_joined
    # Create pub/sub clients for the user who just joined
    initialize_pub_sub
  end

 def initialize_pub_sub
   @@redis_client ||= Redis.new(:driver => :synchrony)

   @publisher ||= Redis.new(:driver => :synchrony)
   @subscriber ||= Redis.new(:driver => :synchrony)
 end


def handle_join(join_data)
  room_name = join_data[:room_name]

  @subscriber.subscribe(room_name) do |on|
    on.message do |channel, message|
      render message
    end
  end

  puts "fetching history items"
  history_items = history_for_room(room_name)
  render "{ \"history\": true, \"time:\": #{Time.now.to_f}, \"data\": [#{history_items.join(',')}] }"
end

handle_join is being called from the on_message event handler of my Cramp action, since cramp supports synchrony and fibers, after a client joins a channel, the @subscriber.subscribe block operates as non-blocking like it should, meaning that it accepts messages fetched on the pubsub channel, and does not block everything else in the process (the application is not blocked), BUT it doesn't move on to the next line ( puts "fetching history items" ) which is a major problem for me, has anyone got a hint on what can be done to rectify this problem?

I didn't find Cramp so useful, and plain Sinatra can be used, here 's an example. Consider adding rack-fiber_pool to the mix. Synchrony adapter didn't work for same purposes ( example ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM