diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-10-16 21:05:33 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-10-16 21:11:21 -0500 |
commit | 84b1f0a3e622d35bf1fb1b2662bc0262a040e119 (patch) | |
tree | cbfef05e91f6e071cc90379ebdf63cf227b91d83 /lib/assets/javascripts | |
parent | df5a32dfbc94723d847aa8d8034041a2bd8751e2 (diff) | |
download | rails-84b1f0a3e622d35bf1fb1b2662bc0262a040e119.tar.gz rails-84b1f0a3e622d35bf1fb1b2662bc0262a040e119.tar.bz2 rails-84b1f0a3e622d35bf1fb1b2662bc0262a040e119.zip |
Send subscription confirmation from server to the client to avoid race conditions.
Without this, it's very easy to send messages over a subscription even before the
redis pubsub has been fully initialized.
Now we delay calling the subscription#connected method on the client side until we
receive a subscription confirmation message from the server.
Diffstat (limited to 'lib/assets/javascripts')
-rw-r--r-- | lib/assets/javascripts/cable.coffee | 2 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/connection.coffee | 10 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/subscriptions.coffee | 6 |
3 files changed, 12 insertions, 6 deletions
diff --git a/lib/assets/javascripts/cable.coffee b/lib/assets/javascripts/cable.coffee index 0bd1757505..476d90ef72 100644 --- a/lib/assets/javascripts/cable.coffee +++ b/lib/assets/javascripts/cable.coffee @@ -3,6 +3,8 @@ @Cable = PING_IDENTIFIER: "_ping" + INTERNAL_MESSAGES: + SUBSCRIPTION_CONFIRMATION: 'confirm_subscription' createConsumer: (url) -> new Cable.Consumer url diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee index 90d8fac3e1..33159130c7 100644 --- a/lib/assets/javascripts/cable/connection.coffee +++ b/lib/assets/javascripts/cable/connection.coffee @@ -52,8 +52,14 @@ class Cable.Connection events: message: (event) -> - {identifier, message} = JSON.parse(event.data) - @consumer.subscriptions.notify(identifier, "received", message) + {identifier, message, type} = JSON.parse(event.data) + + if type? + switch type + when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION + @consumer.subscriptions.notify(identifier, "connected") + else + @consumer.subscriptions.notify(identifier, "received", message) open: -> @disconnected = false diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee index eeaa697081..4efb384ee2 100644 --- a/lib/assets/javascripts/cable/subscriptions.coffee +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -21,13 +21,11 @@ class Cable.Subscriptions add: (subscription) -> @subscriptions.push(subscription) @notify(subscription, "initialized") - if @sendCommand(subscription, "subscribe") - @notify(subscription, "connected") + @sendCommand(subscription, "subscribe") reload: -> for subscription in @subscriptions - if @sendCommand(subscription, "subscribe") - @notify(subscription, "connected") + @sendCommand(subscription, "subscribe") remove: (subscription) -> @subscriptions = (s for s in @subscriptions when s isnt subscription) |