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/cable/connection.coffee | |
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/cable/connection.coffee')
-rw-r--r-- | lib/assets/javascripts/cable/connection.coffee | 10 |
1 files changed, 8 insertions, 2 deletions
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 |