0

When I'm referring to actioncable channels, i am referring to this: actioncable_channel .

After creating the chatroom + chatroom_users, the user has to refresh the page to connect to that specific chatroom_id channel. Is it possible to connect to that channel without a reloading the page?

Raidspec
  • 71
  • 7

1 Answers1

1

It is possible to create connection upon a custom action in your view (instead of page refresh). Please have a look at the code below,

createconusmer = (send_params) ->
  App.chatbot = App.cable.subscriptions.create { channel: "ChatbotChannel" , auth_token: send_params , url: string },
      connected:  ->
        # Called when the subscription is ready for use on the server

      disconnected: ->
        # Called when the subscription has been terminated by the server

      received: (data) ->
        console.log(data)

      speak: (data, responder, payload) ->
          @perform 'speak' , message: data , responder: responder , payload: payload 

Now you can define custom function in your coffee file as,

nameclick = (value) ->
    createconusmer value

window["nameclick"] = nameclick

Now in your view, you can use function nameclick to create a new streaming. Also, I am adding my bit of code to make sure they are unique or not, so to avoid addition of repititive connections.

connections = []

addConnection = (id) ->
  connections.push(id)

removeConnection = (id) ->
  index = connections.indexOf(id)
  connections.splice(index, 1) if index > -1

connectedTo = (id) ->
  connections.indexOf(id) > -1

nameclick = (value) ->
  if connectedTo(value) == false
    addConnection(value)
    createconusmer value
Vaibhav Maheshwari
  • 380
  • 1
  • 3
  • 18