-
-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] AnyCable and Stimulus Reflex #46
Comments
I hope so) Thanks for pinging me.
Interesting:
Need to do some debugging.
Yep, session is not supported out-of-the-box. There is a way to do that like this (from this comment): env['rack.session'] = cookies.encrypted[Rails.application.config.session_options[:key]] That's just an example, a universal solution would be more complicated.
Take a look at: anycable/anycable-rails#95 (comment) |
This may prove helpful. https://www.driftingruby.com/episodes/plugging-in-anycable |
That video is great! Unfortunately, it doesn't address either of the two issues I outlined, I don't think. I was able to get AnyCable running in the general sense after some trial and error; in other words, it functions well as a drop-in enhancement to ActionCable. However, in the context of StimulusReflex, we need a workaround for what I describe as "Worm 1"... consider it a "10/10 flames" category blocker. We might be able to get around the session (aka "Worm 2") but we do rely on it heavily for most of our apps. I hope to be able to dig in deeper on this later in the month. However, nothing can really move forward until I get some help on the 1st concern. |
Provide a Rack env stub instead Related to stimulusreflex/stimulus_reflex#46
We're currently working on the out-of-the-box session support. That should eliminate the 🐛 №2 (if we work it out). As for the first one, I figured out why it's |
Great news, @palkan! Whatever you did seemed to work. I have SR + AnyCable up and running with local source versions of whatever was current last night. I am currently attempting to figure out how to access ActionController.cookies outside of a controller, if anyone has any magic scrolls handy. Rails source is clear as mud. |
So far, I've been able to get to:
This returns nil, which is an improvement on all of the other things I've tried. Help! 😀 |
You can try this branch: anycable/anycable-rails#111 It makes using sessions transparent (it's currently under review and will be merged into v1.0 development branch soon). |
@palkan hey again! Thanks for the PR. I checked it out and so far I’m not seeing what we’re looking for. Is there something I could be missing, like obvious in hindsight gotchas? It’s just returning nil. There’s a second issue as well: in the code it’s clear you have to call session.fetch(“value”, nil) instead of session.value. This would still be a successful outcome: we could explain to people that if they want to use session, they have to convert their code to use fetch. However, is not a “drop-in”. Is it possible to also support the shorthand syntax? |
@palkan any plans to release new version of AnyCable with anycable/anycable-rails#111 seems like this will be important fix for those of us running into the same issue and would make AnyCable as a drop-in replacement for SR |
Hey everyone! I've release the first preview version of the upcoming AnyCable v1.0, which includes the PR mentioned above and other changes which improve StimulusReflex compatibility. You can give it a try by doing the following:
This version successfully runs all StimulusReflexExpo demos but one ("snake" is hardly possible to implement with short-lived connections, since it spawns tons of threads; I don't think it makes a lot of sense to spend a lot of time to make it compatible with AnyCable, 'cause I hope you do not do stuff like that in production 🙂). And feedback is appreciated! |
🐍 🐍 🐍 You don't like snakes!? Thank you so much for getting this out the door and your advice along the way, Vladimir. You rock. |
@palkan a quick follow-up question: you mentioned that snake won't work because it spawns tons of threads. What is the specific reason that snake doesn't work? Is it because it's spawning threads or some other factor? eg. Can I spawn one thread? Can I spawn n threads slowly? Obviously the goal isn't to fix 🐍 but I do need to document any caveats for people using the libraries together. |
It relies on the So, it's OK to use session in threads if you're not going to update it (the example from the documentation would work fine). Another option is to reload the session before reading it from the thread (you can do that by calling |
@palkan perfect answer. Thank you again! |
Hi @palkan I am testing something with Stimulus Reflex, and for me the session.send(:load!) doesn't work, I get |
I'm getting a similar error using AnyCable:
Which is happening here, in reflex.rb:37:
stimulus_reflex 3.1.4 |
It seems that Rails' Fixed in |
Hi, When performing an action my reflex is being executed, but I get an error:
any idea how to solve this? #app/channels/application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
ckey = Rails.application.config.session_options[:key] || raise("No cookies key in config")
redis_key = "#{ENV['APP_SESSION_REDIS_PREFIX']}#{cookies[ckey]}"
env["rack.session"] = JSON.parse(REDIS_SESSIONS.get(redis_key))
user = Warden::SessionSerializer.new(env).fetch(:user)
reject_unauthorized_connection unless user
User.where(id: user.id).first
end
end
end # my example reflex
class PartnerArea::ExampleReflex < ApplicationReflex
delegate :current_user, to: :connection
def example_action
puts "example_action REFLEX action running".green
puts "current_user.id=#{current_user.id}"
end
end Thank you for your help! |
Hey guys, I finally managed to make it work. It was missing to configure the middleware: #config/environment/development.rb
# https://github.com/anycable/anycable-rails/issues/127#issue-624598894
AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|
Devise.warden_config = config
end Thanks. |
Sorry you had trouble, but thanks for documenting this (however unintentionally). I've borrowed the link to the AnyCable issue for the next release of the SR docs. |
@leastbad You can also add a link to the corresponding AnyCable documentation article 😉: https://docs.anycable.io/v1/#/ruby/authentication Also, please, check out the doc on Stimulus Reflex (it’s a very simple early version I’ve just pushed yesterday), maybe, you have something to add: https://docs.anycable.io/v1/#/ruby/stimulus_reflex |
@palkan thanks so much for writing that up! I had no idea you'd done that. I have done my best to work in the details to https://docs.stimulusreflex.com/deployment#anycable as well. I'm a bit fuzzy on Truth be told, I don't have any specific feedback re: the instructions you gave beyond that you should feel welcome to borrow anything on our page for yours. I'm not concerned if they are basically mirror images of each other... both serve to advertise the other regardless of how people find them. Finally, give us a heads-up when you're pushing a new version! You're always welcome on Discord but even a note here helps us keep things from falling through the cracks. |
TBH, I haven't check v3 yet. Add added this feature while making Awesome!
Agree. I think, cross-linking them would be a good idea. |
@palkan please let me know ASAP if you ever find out that the option is causing harm. |
Here is the Expo running on AnyCable: https://github.com/anycable/stimulus_reflex_expo I found one bug (so, 1.0.0.rc1 could not work correctly, |
@palkan yikes! I took out the reference to |
Agree, it makes sense. Released.
Only |
First time a Reflex is invoked i'm getting this error |
@matedemorphy Could you please provide a bit more context? Do you use Rails 6.1 (and |
@palkan i'm usinig rails 6.0.3. But this is related to that is a multi-tenant app, and i'm using the Apartment gem, there are some additional configurations that should be done, but i couldn't figure out yet, like make it work with dynamic subdomains, among others. |
I'm currently trying to get Stimulus Reflex working using AnyCable as a drop-in replacement for ActionCable. I'm running into some nasty worms. Ideas welcomed, especially from @palkan 🤓
Worm 1: ApplicationCable::Connection.env returns nil
We merge connection.env with our own constructed hash, resulting in us getting an
undefined method 'merge' for nil:NilClass
error.I tried to cheat and just pass our constructed env in to ActionDispatch::Request.new() and it came back with
Missing rack.input
when we tell ActionDispatch to process the request.I temporarily reverted the sample project to use standard ActionCable and normally rack.input contains an instance of
Puma::NullIO
. Unfortunately, this is precisely where my troubleshooting capacity tapers off dramatically.Worm 2: session.id returns undefined method `fetch' for nil:NilClass
Long story short, if I hard-code a session ID from the database for testing purposes, it seems to work fine. So, we don't have access to the session.id when AnyCable is in the picture.
I don't have a lot of experience with other session stores, but I wonder if it's possible to fix this by switching to using Redis instead of cookies?
I did go through the AnyCable Troubleshooting page and reviewed "From Action to Any" but I could find no reference to these particular issues.
So, not quite drop-in yet but I am optimistic that Vladimir will know what to do!
The text was updated successfully, but these errors were encountered: