Using AnyCable with Hotwire

AnyCable is fully compatible with Hotwire applications without the need for any additional configuration. See the getting started guide here.

However, you can squeeze even more power from AnyCable for Hotwire apps by combining several features. Read below.

RPC-less setup

📖 See also JWT identification and “hot streams”.

AnyCable-Go provides a feature called signed streams, which implements the require turbo-rails Action Cable functionality right in the WebSocket server. This means that subscribing to Turbo Streams doesn't require calling a gRPC Rails server.

If you're only using Turbo Streams and don't rely on pure Action Cable, you can simplify your AnyCable configuration (infrastructure, deployment) by switching to signed streams and JWT authentication.

What's the point? Here are the main benefits of going the RPC-less way:

  • Improve application performance by speeding up WebSocket handshakes and commands.
  • Reduce infrastructure burden by removing the need to run a separate service (RPC). Especially helpful on Heroku.
  • Open the possibility of using Turbo Streams without Rails and even Ruby!

The default flow with AnyCable RPC looks like this:

RailsAnyCableRPCAnyCableGoClientApplicationCable::Connection#connectTurbo::StreamChannel#subscribeloopaction_cable_meta_tag1HTTP Connect2gRPC Connect3Status OK4Welcome5turbo_stream_from signed_stream_id6Subscribe to signed_stream_id7gRPC Command subscribe8Status OK9Subscription Confirmation10ActionCable.server.broadcast11Deliver <turbo-stream>12RailsAnyCableRPCAnyCableGoClient

Compare this with the RPC-less configuration which has the aforementioned features:

RailsAnyCableGoClientVerify JWT and store identifiersVerify Signed Stream ID and Subscribeloopaction_cable_meta_tag_with_jwt(user: current_user)1HTTP Connect2Welcome3turbo_stream_from signed_stream_id4Subscribe to signed_stream_id5Subscription Confirmation6ActionCable.server.broadcast7Deliver <turbo-stream>8RailsAnyCableGoClient

🎥 Check out this AnyCasts screencast—it's a video guide on setting up Hotwire with AnyCable in the RPC-less way: Exploring Rails 7, Hotwire and AnyCable speedy streams.

Step-by-step guide on going RPC-less

# anycable.yml
production:
  jwt_id_key: "some-secret-key"
  • Configure the Turbo Streams verifier key:
# config/environments/production.rb
config.turbo.signed_stream_verifier_key = "s3cЯeT"
  • Disable Disconnect RPC calls and enable JWT identification and signed streams in AnyCable-Go:
ANYCABLE_JWT_ID_KEY=some-secret-key \
ANYCABLE_TURBO_RAILS_KEY=s3cЯeT \
ANYCABLE_DISABLE_DISCONNECT=true \
anycable-go

# or via cli args
anycable-go --jwt_id_key=some-secret-key --turbo_rails_key=s3cЯeT --disable_disconnect

NOTE: Disabling Disconnect calls is safe unless you have some custom logic in the ApplicationCable::Connection#disconnect method. Otherwise, you still need to run the RPC server.