You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WebsocketRails::BaseController offers the accept_channel and
deny_channel methods to help with channel authorization. They will
trigger the appropriate callback on the channel.
You can also pass an object to the accept and deny methods that will be passed to the success or failure callback
functions on the client channel.
Here is an example of handling the authorization in your controller using the CanCan Gem.
classAuthorizationController < WebsocketRails::BaseControllerdefauthorize_channels# The channel name will be passed inside the message Hashchannel=WebsocketRails[message[:channel]]ifcan?:subscribe,channelaccept_channelcurrent_userelsedeny_channel({:message=>'authorization failed!'})endendend
On the client side, you can use the dispatcher.subscribe_private()
method to subscribe to a private channel.
varprivate_channel=dispatcher.subscribe_private('channel_name',function(){// success callbackconsole.log(current_user.name+"Has joined the channel");},function(reason){// failure callbackconsole.log("Authorization failed because "+reason.message);});
By default, all subscribers in to a channel will be removed when that channel is made private. If you don't wish active subscribers to be removed from a previously public channel when making it private, uncomment and set the following in config/initializers/websocket_rails.rb to true.
config.keep_subscribers_when_private=true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -60,3 +60,9 @@ var private_channel = dispatcher.subscribe_private('channel_name', function() {
console.log( "Authorization failed because "+reason.message );
});
```
By default, all subscribers in to a channel will be removed when that channel is made private. If you don't wish active subscribers to be removed from a previously public channel when making it private, uncomment and set the following in config/initializers/websocket_rails.rb to true.
There are no files selected for viewing
Private channels give you the ability to authorize a user's subscription using the authorization mechanism of your choice.
You can specify which channels you wish to make private inside of the event router using the
private_channel
method.Or you can always mark any channel as private later on from anywhere inside of your Rails application.
Handling the authorization is simple as well. Just subscribe the
websocket_rails.subscribe_private
event to the controller and action of your choice.WebsocketRails::BaseController
offers theaccept_channel
anddeny_channel
methods to help with channel authorization. They will trigger the appropriate callback on the channel.You can also pass an object to the accept and deny methods that will be passed to the success or failure callback functions on the client channel.
Here is an example of handling the authorization in your controller using the CanCan Gem.
On the client side, you can use the
dispatcher.subscribe_private()
method to subscribe to a private channel.By default, all subscribers in to a channel will be removed when that channel is made private. If you don't wish active subscribers to be removed from a previously public channel when making it private, uncomment and set the following in config/initializers/websocket_rails.rb to true.