Consider a Rails app using Action Cable. When a client subscribes to a channel, what is the immediate effect on the server side?
Think about how Action Cable manages connections and data flow after subscription.
When a client subscribes, the server creates a subscription instance. It does not send data immediately but waits for broadcasts or client messages to push updates.
Which of the following lines correctly broadcasts a JSON message {status: 'ok'} to a channel named 'NotificationsChannel'?
Remember the method to send data from server to all subscribers of a channel.
The correct method is ActionCable.server.broadcast which sends data to all subscribers of the given channel.
Given this subscription code in app/channels/chat_channel.rb:
class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from 'chat_channel'
end
endAnd this broadcast call in a controller:
ActionCable.server.broadcast('chat_channel', message: 'Hello')Why might the client not receive the broadcast?
Check the exact string used in stream_from and in the broadcast call.
Action Cable channel names are case sensitive. The subscription streams from 'chat_channel' (lowercase), and the broadcast uses 'chat_channel' (lowercase) so they match. If there was a case mismatch, the client would not receive the broadcast.
In an Action Cable connection, what happens to the connection object state after the client disconnects?
Think about how WebSocket connections behave on disconnect and reconnect.
When a client disconnects, the connection is closed but the client can reconnect automatically, establishing a new connection object.
In a production environment with multiple Rails server instances, how does Action Cable ensure that broadcasts reach all subscribed clients regardless of which server instance they are connected to?
Consider how messages are shared between servers in a distributed system.
Action Cable uses Redis as a pub/sub backend to broadcast messages to all server instances, ensuring all clients receive updates regardless of which server they connect to.
