Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ActionCable from 'actioncable'
- const buildUrl = (url, parameters) {
- var qs = ''
- for (var key in parameters) {
- var value = parameters[key]
- qs += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&'
- }
- if (qs.length > 0) {
- qs = qs.substring(0, qs.length - 1)
- url = url + '?' + qs
- }
- return url
- }
- const tokens = {
- 'access-token': 'xxx',
- 'client': 'xxx',
- 'uid': 'xxx'
- }
- const wsUrl = buildUrl(
- 'ws://localhost:3000/cable',
- tokens
- )
- const consumer = ActionCable.createConsumer(wsUrl)
- const channel = consumer.subscriptions.create({
- channel: 'HogeChannel'
- }, {
- connected: (o) => console.log(o),
- disconnected: (o) => console.log(o),
- received: (o) => console.log(o),
- rejected: (o) => console.log(o)
- })
- // Send action
- channel.perform('hoge')
Add Comment
Please, Sign In to add comment