1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Change ActionCable.server.broadcast to XChannel.broadcast_to

Using broadcast directly off server is not recommended
This commit is contained in:
David Kuhta 2016-02-23 20:21:05 -06:00
parent 55d9a70634
commit e39d8dd506

View file

@ -8,7 +8,7 @@ After reading this guide, you will know:
* How to setup Action Cable * How to setup Action Cable
* How to setup channels * How to setup channels
b
Introduction Introduction
------------ ------------
@ -189,11 +189,10 @@ get the broadcast should they connect later.
Broadcasts are called elsewhere in your Rails application: Broadcasts are called elsewhere in your Rails application:
```ruby ```ruby
ActionCable.server.broadcast \ WebNotificationsChannel.broadcast_to current_user, title: 'New things!', body: 'All the news fit to print'
"web_notifications_#{current_user.id}", { title: 'New things!', body: 'All the news that is fit to print' }
``` ```
The `ActionCable.server.broadcast` call places a message in the current The `WebNotificationsChannel.broadcast_to` call places a message in the current
subscription adapter (Redis by default)'s pubsub queue under a separate subscription adapter (Redis by default)'s pubsub queue under a separate
broadcasting name for each user. For a user with an ID of 1, the broadcasting broadcasting name for each user. For a user with an ID of 1, the broadcasting
name would be `web_notifications_1`. name would be `web_notifications_1`.
@ -267,8 +266,7 @@ App.cable.subscriptions.create { channel: "ChatChannel", room: "Best Room" },
```ruby ```ruby
# Somewhere in your app this is called, perhaps from a NewCommentJob # Somewhere in your app this is called, perhaps from a NewCommentJob
ActionCable.server.broadcast \ ChatChannel.broadcast_to chat_#{room}, sent_by: 'Paul', body: 'This is a cool chat app.'
"chat_#{room}", { sent_by: 'Paul', body: 'This is a cool chat app.' }
``` ```
@ -285,7 +283,7 @@ class ChatChannel < ApplicationCable::Channel
end end
def receive(data) def receive(data)
ActionCable.server.broadcast "chat_#{params[:room]}", data ChatChannel.broadcast_to "chat_#{params[:room]}", data
end end
end end
``` ```
@ -430,11 +428,10 @@ App.cable.subscriptions.create "WebNotificationsChannel",
```ruby ```ruby
# Somewhere in your app this is called, perhaps from a NewCommentJob # Somewhere in your app this is called, perhaps from a NewCommentJob
ActionCable.server.broadcast \ WebNotificationsChannel.broadcast_to current_user, title: 'New things!', body: 'All the news fit to print'
"web_notifications_#{current_user.id}", { title: 'New things!', body: 'All the news that is fit to print' }
``` ```
The `ActionCable.server.broadcast` call places a message in the current The `WebNotificationsChannel.broadcast_to` call places a message in the current
subscription adapter (Redis by default)'s pubsub queue under a separate subscription adapter (Redis by default)'s pubsub queue under a separate
broadcasting name for each user. For a user with an ID of 1, the broadcasting broadcasting name for each user. For a user with an ID of 1, the broadcasting
name would be `web_notifications_1`. name would be `web_notifications_1`.