Reputation: 641
I have discovered ActionCable today and I have created a very simple chat. Now I want to add this into my current project.
When is a connection with a channel established? In my simple chat, I have a controller named Welcome with an index method and a channel named Demo. On the index page, you can write/see messages. I deduce when we access any page of the app, we are automatically connected to the channel? (if i don't add any instructions in connection.rb)
Upvotes: 2
Views: 397
Reputation: 13531
When you load the web page.
The client opens the connection to the actioncable channel. According to the docs in the client side section:
http://edgeguides.rubyonrails.org/action_cable_overview.html#client-side-components
When you load the web page (in your case, the welcome controller's index action) javascript such as this will execute:
// app/assets/javascripts/cable.js
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
Followed by a subscription function such as:
# app/assets/javascripts/cable/subscriptions/chat.coffee
App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" }
That is what opens the connection to your channel which is defined (as in this example in the server side channels section): http://edgeguides.rubyonrails.org/action_cable_overview.html#channels
# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
# Called when the consumer has successfully
# become a subscriber of this channel.
def subscribed
end
end
When the client side javascript subscribes, the connection will eventually lead to the subscribed
method of your channel object.
Upvotes: 2