From 6d488a22d361d19e3de98c603d98e64ccea8a2f3 Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Sun, 31 Mar 2019 13:41:12 -0400 Subject: feat(js): Dynamic ActionCable URL (#35579) * Failing test case * feat: Dynamic Url Generation Change createWebSocketURL to be a closure that allows url to be evaluated at the time the webSocket is established * refactor: createWebSocketURL to Consumer, remove need for closure Move initial call to createWebSocketURL in createConsumer * docs: Add documentation for dynamic url and string args to createConsumer Co-Authored-By: rmacklin [Ryan Castner, rmacklin] --- guides/source/action_cable_overview.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'guides/source') diff --git a/guides/source/action_cable_overview.md b/guides/source/action_cable_overview.md index c531b6eee2..7bf6a73797 100644 --- a/guides/source/action_cable_overview.md +++ b/guides/source/action_cable_overview.md @@ -190,6 +190,23 @@ This will ready a consumer that'll connect against `/cable` on your server by de The connection won't be established until you've also specified at least one subscription you're interested in having. +The consumer can optionally take an argument that specifies the url to connect to. This +can be a string, or a function that returns a string that will be called when the +WebSocket is opened. + +```js +// Specify a different url to connect to +createConsumer('https://ws.example.com/cable') + +// Use a function to dynamically generate the url +createConsumer(getWebSocketURL) + +function getWebSocketURL { + const token = localStorage.get('auth-token') + return `https://ws.example.com/cable?token=${token}` +} +``` + #### Subscriber A consumer becomes a subscriber by creating a subscription to a given channel: -- cgit v1.2.3