I've got a Rails application with a PubSub Server (Faye) as middleware.. I've got the usual Rails-structure for Models, Views and Controllers, and I've got some controllers for my Socket-channels.. (Provided by FayeRails)

The problem: I need to share a client list between my socket controllers and my general controllers. This is because the authentication is done via a Rails controller (so I'm able to use sessions)..

Normally I would put this kind of stuff in my ApplicationController so all inherited controllers and views can get to it, but the socket controllers are inherited from FayeRails::Controller so that not an option.. I have no clue where the instances of these controllers go.. Also, I can't edit the initialize because all the controllers are setup automatically by Rails and the FayeRails gem. I tried using globals, but that feels wrong.. Also I've been thinking of ActiveRecord, but it doesn't feel right to add fast-changing data to a database.. Lastly I though of an ActiveRecord-like class that holds the list, but this feels the same as a global..

I can't really think of any other options to share the client list between these two controllers..

What would be a nice and clean way for doing this?

有帮助吗?

解决方案

You could put what you need in a module in the lib directory. The include it in your application_controller and then extend the main FayeRails controllers, include the module in there as well. To extend, just created a new one with the same name in your controller file, maybe sure the class name is the same, then require it in your config/initializers/extensions.rb file.

Example

# config/initializers/extensions.rb
require "#{Rails.root}/app/controllers/whatever_controller.rb"

As for speed, yeah, if you're worried about that I would look into keeping what you need in a persistent redis DB. But if FayesRails uses ActiveRecord methods I'm not sure how easy/hard that would be.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top