Performance: Action Cable for real-time updates
Action Cable impacts the page's interaction responsiveness and network usage by enabling real-time data updates without full page reloads.
class ChatChannel < ApplicationCable::Channel def subscribed stream_from 'chat_channel' end def receive(data) # Broadcast only complete messages, not every keystroke if data['message'].present? ActionCable.server.broadcast('chat_channel', data) end end end # Client-side: sends message only on submit
class ChatChannel < ApplicationCable::Channel def subscribed stream_from 'chat_channel' end def receive(data) ActionCable.server.broadcast('chat_channel', data) end end # Client-side: broadcasts every keystroke as a message
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Broadcast every keystroke | Many small DOM updates | Multiple reflows per second | High paint cost due to frequent changes | [X] Bad |
| Broadcast on message submit | Few DOM updates | Single reflow per message | Low paint cost | [OK] Good |
subscriptions.create.subscriptions.create('ChatChannel'), which is the correct syntax.class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from 'chat_room'
end
endApp.cable.subscriptions.create('ChatChannel', {
received(data) {
console.log(data.message)
}
}){ message: 'Hello!' } to 'chat_room'?class NotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from notifications
end
endstream_from must be a string representing the stream name, e.g., 'notifications'.notifications without quotes, treating it as a variable, which causes an error.ActionCable.server.broadcast(stream, data).ActionCable.server.broadcast with stream name and data hash, which is correct.Pass the test and this pattern moves unknown