Skip to content

Commit

Permalink
Use a separate action cable server for hotwire-spark
Browse files Browse the repository at this point in the history
This will isolate the connection from hotwire-spark from the connection
used by the host app, which most likely will be authenticated. This fixes
the problem where hotwire-spark doesn't work with unauthenticated connections.

Fixes #19

This also lets us remove lots of complexity for the system monkeypatching
the cable server to prevent restarts, since we don't want those when code reloads
in development.
  • Loading branch information
jorgemanrubia committed Dec 20, 2024
1 parent adb851d commit 13a3cdf
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 84 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ var HotwireSpark = (function () {
}
}

var consumer = createConsumer();
var consumer = createConsumer("/hotwire-spark");

function assetNameFromPath(path) {
return path.split("/").pop().split(".")[0];
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/javascript/hotwire/spark/channels/consumer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createConsumer } from "@rails/actioncable"

export default createConsumer()
export default createConsumer("/hotwire-spark")
6 changes: 6 additions & 0 deletions lib/hotwire-spark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@ def install_into(application)
def enabled?
enabled && defined?(Rails::Server)
end

def cable_server
@server ||= Hotwire::Spark::ActionCable::Server.new(config: ::ActionCable::Server::Base.config.dup).tap do |server|
server.config.connection_class = -> { ::ActionCable::Connection::Base }
end
end
end
end
43 changes: 0 additions & 43 deletions lib/hotwire/spark/action_cable/persistent_cable_middleware.rb

This file was deleted.

25 changes: 0 additions & 25 deletions lib/hotwire/spark/action_cable/persistent_cable_server.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/hotwire/spark/action_cable/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Hotwire::Spark::ActionCable::Server < ActionCable::Server::Base
end
10 changes: 9 additions & 1 deletion lib/hotwire/spark/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ class Engine < ::Rails::Engine
html_paths: %w[ app/controllers app/helpers app/models app/views ],
stimulus_paths: %w[ app/javascript/controllers ]

initializer "hotwire_spark.config" do |app|
initializer "hotwire_spark.config" do |application|
config.hotwire.spark.each do |key, value|
Hotwire::Spark.send("#{key}=", value)
end
end

initializer "hotwire_spark.action_cable.routes" do
config.after_initialize do |app|
app.routes.prepend do
mount Hotwire::Spark.cable_server => "/hotwire-spark", internal: true, anchor: true
end
end
end

initializer "hotwire_spark.install" do |application|
Hotwire::Spark.install_into application if Hotwire::Spark.enabled?
end
Expand Down
5 changes: 1 addition & 4 deletions lib/hotwire/spark/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ def install
end

def configure_middleware
::ActionCable::Server::Base.prepend(Hotwire::Spark::ActionCable::PersistentCableServer)

middleware.insert_before ActionDispatch::Executor, Hotwire::Spark::ActionCable::PersistentCableMiddleware
middleware.use Hotwire::Spark::Middleware
end

Expand All @@ -39,7 +36,7 @@ def monitor(paths_name, action:)
end

def broadcast_reload_action(action, file_path)
ActionCable.server.broadcast "hotwire_spark", reload_message_for(action, file_path)
Hotwire::Spark.cable_server.broadcast "hotwire_spark", reload_message_for(action, file_path)
end

def reload_message_for(action, file_path)
Expand Down
7 changes: 0 additions & 7 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

require "helpers/files_helper"

::ActionCable::Server::Base.prepend(Hotwire::Spark::ActionCable::PersistentCableServer)

class ActiveSupport::TestCase
include FilesHelper

setup do
reload_rails_reloader
ActionCable.server.suppress_restarts = true
end

teardown do
ActionCable.server.suppress_restarts = false
end

private
Expand Down

0 comments on commit 13a3cdf

Please sign in to comment.