12. app/channels/rooms_channel.rb
class RoomsChannel < ApplicationCable::Channel
def subscribed
stream_from 'room_sorocaba_channel' # can be dynamic
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def send_message(data)
ActionCable.server.broadcast('room_sorocaba_channel', data)
end
end
14. app/assets/javascripts/channels/rooms.js
App.rooms = App.cable.subscriptions.create("RoomsChannel", {
connected: function() {
// Called when the subscription is ready for use on the server
},
disconnected: function() {
// Called when the subscription has been terminated by the server
},
received: function(data) {
// Called when there's incoming data on the websocket for this channel
},
send_message: function() {
return this.perform('send_message');
}
});