Skip to content

Commit

Permalink
feat: use AnyCable
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Oct 28, 2019
1 parent 12bd0be commit 9d7e06e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Efficient ActionCable replacement to use web-sockets
gem 'anycable-rails', '~> 0.6'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

Expand All @@ -31,6 +34,10 @@ group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'pry-rails'

# AnyCable Web Socket server
# Disabled. Gem is very unstable.
# gem "anycable-rack-server"
end

group :development do
Expand Down
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
anycable (0.6.3)
anyway_config (~> 1.4.2)
grpc (~> 1.17)
anycable-rails (0.6.4)
anycable (~> 0.6.0)
rails (>= 5)
anyway_config (1.4.4)
arel (9.0.0)
bindex (0.8.1)
bootsnap (1.4.4)
Expand All @@ -55,6 +62,12 @@ GEM
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
google-protobuf (3.9.2)
googleapis-common-protos-types (1.0.4)
google-protobuf (~> 3.0)
grpc (1.24.0)
google-protobuf (~> 3.8)
googleapis-common-protos-types (~> 1.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jbuilder (2.9.1)
Expand Down Expand Up @@ -153,6 +166,7 @@ PLATFORMS
ruby

DEPENDENCIES
anycable-rails (~> 0.6)
bootsnap (>= 1.1.0)
byebug
jbuilder (~> 2.5)
Expand Down
8 changes: 7 additions & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ class Connection < ActionCable::Connection::Base
identified_by :current_user

def connect
self.current_user = request.session.fetch("username", nil)
self.current_user = session.fetch("username", nil)
reject_unauthorized_connection unless current_user
end

# By default we don't have access to the session object
# https://edgeguides.rubyonrails.org/action_cable_overview.html#notes
def session
cookies.encrypted[Rails.application.config.session_options[:key]]
end
end
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>EvilChat</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= action_cable_meta_tag %>

<%= stylesheet_pack_tag 'application', media: 'all' %>
</head>
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

config.action_cable.url = ENV.fetch("CABLE_URL") { "ws://localhost:3334/cable" }

config.generators do |g|
# Don't generate assets for Sprockets
g.assets = nil
Expand Down
11 changes: 7 additions & 4 deletions config/cable.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
default: &default
adapter: <%= ENV.fetch('CABLE_ADAPTER', 'any_cable') %>

development:
adapter: async
<<: *default

test:
adapter: async
<<: *default
channel_prefix: evil_chat_test

production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
<<: *default
channel_prefix: evil_chat_production
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

# Always run gRPC server from anycable-rack-server alongside the app's server
# config.any_cable_rack.run_rpc = true
end
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ services:
- postgres
- redis
- webpacker
- anycable-ws
- anycable-rpc

webpacker:
<<: *app
Expand All @@ -64,6 +66,24 @@ services:
RAILS_ENV: ${RAILS_ENV:-development}
WEBPACKER_DEV_SERVER_HOST: 0.0.0.0

anycable-ws:
image: anycable/anycable-go:v0.6.4
ports:
- '3334:3334'
environment:
PORT: 3334
REDIS_URL: redis://redis:6379/0
ANYCABLE_RPC_HOST: anycable-rpc:50051
depends_on:
- anycable-rpc
- redis

anycable-rpc:
<<: *backend
command: bundle exec anycable
ports:
- '50051'

postgres:
image: postgres:12
volumes:
Expand Down

0 comments on commit 9d7e06e

Please sign in to comment.