DEV Community

Cover image for My Struggle With Rails ActionCable

My Struggle With Rails ActionCable

shakycode on February 02, 2017

Let me give you a bit of background on this article. I'm building a multi-tenant SaaS platform that connects Physicians with Patients. Included in ...
Collapse
 
blueplanet profile image
blueplanet

Thank's for you content!!!

And , in the channel.rb , i use follwing code:

class UpdateNoticeChannel < ApplicationCable::Channel
  def subscribed
    tenant = self.connection.env['HTTP_HOST'].split('.').first
    Apartment::Tenant.switch! tenant

    stream_from "update_notice:tenant[#{tenant}]:user[#{params[:user_id]}]"
  end
end

Collapse
 
stevensonmt profile image
stevensonmt

Total newb so I may not fully understand the problem, but this might be something the HyperReact tools like HyperMesh could help with. Especially if you end up using polling anyway.

Collapse
 
dsh profile image
Daya Sharma

Can a User in your multi-tenant app use more than one subdomain? If the answer is no then do you have a relationship between User & Account or User & Subdomain, if yes then you can switch the tenant context based to the subdomain assigned to the user. No ?

How about using ActsAsTenant multi-tenancy gem which stores tenant context in the current thread and using ActsAsTenant.current_tenant = request.subdomain in Connection you should be able to access ActsAsTenant.current_tenant is Connection.

May be I am missing something here ... so let me know if I am wrong.

Collapse
 
abrisse profile image
Aymeric Brisse

That's exactly what I thought. It should definitely work.

See github.com/rails/rails/issues/27875

Collapse
 
matedemorphy profile image
Joseph Palmezano

My struggle with ActionCable and Apartment Gem it's simply doesn't works.

/ [ActionCable] [User user1@example.com] [Tenant user101] Registered connection (xxxxxxxxxxxxxxxxxx)
prints every 2-3 seconds in console, but ActionCable.server.broadcast("test", "ActionCable broadcast!") it does not do anything.

Collapse
 
jamesdesbyrne profile image
James Byrne 🏹

Great piece and an interesting problem. At work we started off using action cable for a live log so you could see your requests going through our system in your browser. We were already using redis as a comms layer between the Ruby on Rails app and the erlang simulator so it was easy enough to setup the channels and start listening. We even managed to dynamically set the channel names albeit we where setting the users api key and not changing domains.

Ultimately though we ended up using a really small Phoenix application just to manage the channels (request counters and live log). We had to keep redis for rails/erlang comms and listen on that but overall the Phoenix version is much more performant than the rails version was. Hope you get the issue fixed though actioncable has a lot of promise and serves great for the majority of applications.

Collapse
 
shakycode profile image
shakycode

Thanks for chiming in, James. Hey... we're both James!

I've also looked into Elixir/Phoenix for solving this problem and I may move to that framework/language for this particular platform.

ActionCable is amazing but I've yet to see a multi-tenant platform integrate ActionCable yet. Surely I can't be the only use case on the Internet? Granted AC is brand new with Rails 5 so I'm sure as time progresses the kinks will be worked out.

My goal over the next week is to tear AC apart and figure out how to get the request passed from the connection to the channel. I'm hoping to do a PR against Rails core soon that will introduce the request into the channel, but the Rails community is picky about PRs and use cases. So we'll see :)