Plugging in AnyCable

Episode #214 by Teacher's Avatar David Kimura

Summary

ActionCable can handle quite a bit of traffic, but it gets to a point where it can become a burden on the server. With AnyCable, we can not only handle more traffic, but it is also lighter on the resources.
websockets rails 9:06

Resources

Summary

# Terminal
rails g channel welcome

brew install anycable-go

anycable-go --host=localhost --port=3334
anycable

bundle exec anycable --server-command "anycable-go --host=localhost --port=3334"

# app/javascript/channels/welcome_channel.js
received(data) {
  // Called when there's incoming data on the websocket for this channel
  console.log(data)
}

# app/channels/welcome_channel.rb
def subscribed
  stream_from "welcome_channel"
end

# controllers/welcome_controller.rb
def index
  ActionCable.server.broadcast('welcome_channel', Time.now)
end

# Gemfile
gem "anycable-rails"
gem "redis"

# config/cable.yml
development:
  adapter: any_cable

# config/anycable.yml
development:
  redis_url: redis://localhost:6379/1
  access_logs_disabled: false

# config/environments/development.rb
config.action_cable.url = "ws://localhost:3334/cable"

# app/views/layouts/application.html.erb
<%= action_cable_meta_tag %>