Is redis essential to use actioncable in heroku? - ruby-on-rails

I made live chat using actioncable. This works perfectly locally. However, heroku's view / page does not render.
Is redis essential for heroku to work?
I also wrote the code in product.rb
config.action_cable.allowed_request_origins = ['https://my-url-45158.herokuapp.com', 'http://my-url-45158.herokuapp.com']
config.web_socket_server_url = "wss://my-url-45158.herokuapp.com/cable"

afaik yes it is, see this,
https://blog.heroku.com/real_time_rails_implementing_websockets_in_rails_5_with_action_cable
and this
http://edgeguides.rubyonrails.org/action_cable_overview.html#configuration
when in production mode rails use redis for its subscription adapter.
as written in the documentation
by default redis for production and async for development and test environments
beside that there is PostgreSQL Adapter and Async Adapter(but should not be used for production)

Related

Rails 6 app with stimulus reflex deployment on Heroku problem

I deploy my Rails 6 app (with stimulus-relflex) on Heroku. Everything work find including my jQuery and JS but anything related to stimulus-relfex dont work? Everything work fine on my local dev machine. Any ideas?
You give very little information, but my first guess would be to check the config/cable.yml. It is misleading, but ActionCable "just works" in development, while in production mode one has to configure a pubsub queueing system. The default is set to "redis", which is a (payable?) add-on on heroku, and if you already use redis, it has to be configured/linked correctly in the cable.yml.
A very simple solution is to use postgresql as the pubsub queue, which you are probably already using (on heroku), and is as simple as writing
production:
adapter: postgresql
Add the redis addon on heroku to your application maybe that works..

Google App Engine + Ruby on rails + Redis + actioncable

How we should configure action cable in Rails to work in Google App Engine?
I have a Rails application which is using action cable for its messenger.
On my local-host the action cable working well in development and production environments but its not working on Google App Engine.
I have created a Computer Engine instance and installed Redis on it.
Firewall allows all traffic to Redis VM.
Redis has bind 0.0.0.0
When I deploy the app on Google App Engine I get the error:
Firefox can’t establish a connection to the server at wss://mydomain.com/cable.
here is my current configuration:
config/environments/production.rb
config.action_cable.url = 'wss://[MYDOMAIN].com/cable'
config.action_cable.disable_request_forgery_protection = true
config.force_ssl = true
...
config/cable.yml
production:
adapter: redis
url: redis://[IP_OF_REDIS_SERVER]:6379/
app.yaml
entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby
env_variables:
REDIS_PROVIDER: REDIS_URL
REDIS_URL: redis://[IP_OF_REDIS_SERVER]:6379/
SECRET_KEY_BASE: [My_Secret_Key]
I couldn't find anything about actioncable setting in Google documentation of App Engine. I hope this question can help me and everyone with same issue.
You must set create a VPC between your Redis instance and you App Engine instance. This is documented here:
https://cloud.google.com/appengine/docs/standard/python/connecting-vpc
Once you've done this, you'll have:
app.yaml
...
vpc_access_connector:
name: projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME
...
Secondly, the Google managed Redis server does not allow you to set a client id. I tried forcing it to be nil, but the ActionCable subscription adapter will try to set one for you:
https://github.com/rails/rails/blob/6-0-stable/actioncable/lib/action_cable/subscription_adapter/redis.rb#L18
In order to get it working properly, I had to override this behavior.
I added the following file into my rails app:
config/initializers/decorators/redis.rb
require 'action_cable/subscription_adapter/redis'
ActionCable::SubscriptionAdapter::Redis.class_eval do
cattr_accessor :redis_connector, default: ->(config) do
::Redis.new(config.except(:id, :adapter, :channel_prefix))
end
end
Now everything is working fine. My only concern now is that the client id is somehow required and I'm going to end up with some nasty bug due to it.
Hope this works for you if you try it out!

Rails app deploy on Heroku

I'm new in rails app development I am little bit confusing about rails deployment.
I following this guide below and done at all.
That mean my rails app on production mode?
Need to change Puma RACK_ENV to 'production' in Profile before pushing to heroku?
https://devcenter.heroku.com/articles/getting-started-with-rails4
I believe so. The entire tutorial is setting up your rails server to work in production mode. Using some inductive logic, it seems that the tutorial is assuming you're in production:
"Rails 4 no longer has a static index page in production. When you’re
using a new app, there will not be a root page in production, so we
need to create one. We will first create a controller called welcome
for our home page to live:"
Indicates to me that it's assuming your rails server will be running in production.
To make doubly sure, you can check the heroku logs and look for a line that says:
Rails [your version] application starting in production

Why WEBrick server is faster in production mode rather in development mode? + Rails

I have been developing ruby on rails application since some couple of months. I use the default WEBrick server to run the applications. And I found that when I start the WEBrick server in the development and production modes, the server works more speed for production mode than for the development mode.
Is there any specific reason behind that? Can anybody explain me?
In production mode, a server loads code into the cache, which makes things quick. However, that's not the case in development mode (since you don't want to restart your webrick every time you made a change). Every request loads the according code again, which takes a bit of time.
And the most of all time-eaters is the asset pipeline. In production, you get a compiled version of your assets (javascripts and css) in maybe one or two requests. In development, you get them split, for debugging purpose (based on your environment settings, of course). And because a browser does not handle all requests simultaneously, some assets are loaded after other ones are finished loading. You can watch this behaviour with e.g. firebug's network console. That means: the more assets you have, the longer your page takes to load in development mode.
In dev mode classes are not cached, so Rails reloads all the classes each time you refresh. Also, asset compilation is not done in development (by default), so Rails reloads all the assets (CSS, Javascript etc) each time you refresh.
The difference is between 2 environments. In Rails, there are several environment. Each has his own database configuration and Rails options.
You can use the Rails.env variable to made some different change with particular environment.
By default, the development environment is without all cache and activate the auto-reloading. The production environment is with all cache.
But if you want you can make a production environment like development or development environment like production.
You can add some new specific environment too.
Creating new Environment:
Assuming you want create the hudson environment.
Create a new environment file in config/environments/hudson.rb.
You can start by cloning an existing one, for instance config/environments/test.rb.
Add a new configuration block in config/database.yml for your environment.
That's all.
Now you can start the server
ruby script/server -e hudson
Run the console
ruby script/server hudson
And so on.

Running a Rails site: development vs production

I'm learning Ruby on Rails. At the moment I'm just running my site locally with rails server in the OS X Terminal. What changes when a Rails site is run on a production box?
Is the site still started with rails server?
Any differences with how the db is setup?
Note: I'm running Rails 3.
A rails app can be run in production calling rails server -e production, although 99% of the time you'll be serving on something like passenger or thin instead of WEBrick, which means there's a different command to start the server. (thin start -e production for instance)
This is a complicated question, but the best place to start learning about the differences would be to look at the specific environment.rb files. When rails boots up it starts with the environment file that matches the called environment, ie if you start it in development it begins by loading your development.rb file, or if you're in production it will load the production.rb file. The differences in environments are mostly the result of these differences in the various environment config files.
Basically if a Rails 3.1 app is in production mode, then by default it is not going to be compiling assets on the fly, and a lot of caching will be going on that isn't happening in development. Also, when you get error messages they will be logged but not rendered to the user, instead the static error page from your public directory will be used.
To get more insight into this, I would suggest reading the relevant rails guides:
Rails Initialization Guide: http://guides.rubyonrails.org/initialization.html
Rails Configuration Guide: http://guides.rubyonrails.org/configuring.html
There are two contexts you can use the word "production" here. One of them is running the server in production mode. You can do this locally by,
RAILS_ENV=production ./script/server
The configuration for this is picked up from config/environments/production.rb. Try comparing this file with config/environments/development.rb. There are only subtle differences like caching classes. Development mode makes it easier so that it will respond to any changes you make instantly. Plus there are two different databases (by default) will be used namely yourproject_development and yourproject_production if you choose to run your server in either of these modes.
On the other hand, rails deployment to a production box is something different. You will need to pick your server carefully. You may have to deal with a deployment script may be capistrano. You may also need a load balancer such as netgear. The database also may require a deep consideration like size expectation, master/slave clustering etc.,
Note: I have never used Rails 3. This answer is biased towards 2.3.x.

Resources