What is the list of available callbacks for ActionCable subscription create method?
Asked Answered
H

2

6

I am trying to setup my Rails 5 ActionCable for broadcasting updates to my database. So far I got things going but I realized that the documentation on ActionCable is kind of lacking. For my case, I want to know the list of callback I am allowed to put into the function subscriptions.create().

For example

const consumer = ActionCable.createConsumer();
consumer.subscriptions.create(
    'ChatsChannel'
    {
        received: someCallback,
        connected: otherCallback,
        disconnected: anotherCallback
    }
 )

I noticed that there are appendLine and createLine from

section 5.4 http://guides.rubyonrails.org/action_cable_overview.html

How many more are there? What do they correspond to? This is so different from the usual websocket on Node.js and Python. Using socket.io, I only get 4 options, open, close, error and message. Why does ActionCable seem so unconventional when Rails is supposed to be convention over configuration?

Thanks

Heterologous answered 21/9, 2017 at 7:57 Comment(0)
D
3

Judging from the source code, those are the only three callbacks made available out of the box:

connected

received

disconnected

Deedee answered 20/9, 2018 at 8:28 Comment(0)
P
0

In the section you point in the guide:

# app/assets/javascripts/cable/subscriptions/chat.coffee
App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
  received: (data) ->
    @appendLine(data)

  appendLine: (data) ->
    html = @createLine(data)
    $("[data-chat-room='Best Room']").append(html)

  createLine: (data) ->
    """
    <article class="chat-line">
      <span class="speaker">#{data["sent_by"]}</span>
      <span class="body">#{data["body"]}</span>
    </article>
    """

In this code appendLine and createLine are just callbacks defined there, pointed by @appendLine and @createLine, respectively. It's just CoffeeScript syntax to module more expressively your code.

How many more are there?

Honestly, I'm not sure. I haven't find any JS documentation as extensive as the one for the ruby documentation of Action cable. But appendLine and createLine are NOT defined callbacks, they appear only in that example.

// UPDATE:

The complete list can be found here.

Pincers answered 16/8, 2018 at 15:23 Comment(1)
This does not even make an attempt to answer this question.Cartwheel

© 2022 - 2024 — McMap. All rights reserved.