简体   繁体   中英

How can I have a server side faye client running within my rails app?

So I'm trying to build a chat application with rails and faye. And want to have a server side faye client running inside the rails app, in order to handle disconnect events where a user client quits the browser. So that I can call controller actions within my rails application to delete some data from the database. I'm able to detect the user id with my faye rack app, but need a way to send the data further to rails.

So please help if you know how to do this, or have any other suggestions to how I can accomplish this. A second option I have considered is to send post requests directly from faye, but then I need some way to go about the csrf protection. But I would prefer a server side client which can subscribe to a faye channel.

I have solved this now by using the 'faye-rails' gem and included this into my application.rb file:

config.middleware.delete Rack::Lock
config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25 do
    class RealTime < FayeRails::Controller 
        channel '**' do
            monitor :unsubscribe do
                if channel != '/messages' && channel != '/disconnect'
                    user = channel[6..-1]
                    @members = Members.where("username = '#{user}'")
                    data = {:channels => @members, :user => user}
                    RealTime.publish('/disconnect', data)

                    @members.destroy_all
                end
            end
        end
    end
    add_extension(RealTime.new)    
end

I also have a own channel for each user which I use to get the username and delete data from the database.

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