0

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?

Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • Tried sessions? Or perhaps a rails cache or Redis? – omarvelous Apr 17 '13 at 20:59
  • I've didn't come across any session usage within Faye controllers, never used Redis or the Rails cache, but if that's how these things are solved, I'll be sure to check them out! I'll get back to let you know how they work out.. Thanks! – Tim Baas Apr 17 '13 at 21:32
  • Redis is the way to go! Thanks for your support! – Tim Baas Apr 18 '13 at 08:49

1 Answers1

0

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.

JoshEmory
  • 644
  • 6
  • 20
  • I'll try Redis first than, I can avoid using the build in ActiveRecord callbacks of Faye I guess.. Thanks! – Tim Baas Apr 17 '13 at 21:57
  • Trying Redis ATM, works just right! I think I'm going to wrap the redis logic in a module in the `lib` folder just in case the application grows to keep things organized.. So, thanks again! – Tim Baas Apr 18 '13 at 08:47