I'm trying to implement action cable. This is working well, but I want, when I'm connecting to a channel, to receive some data (here I want to know how many users are connected yet).
I created on my channel a custom method nbUsers :
class OnTheSamePageChannel < ApplicationCable::Channel
@count = 0
def subscribed
...
@count++
stream_for(params[:url])
end
def unsubscribed
...
@count--
end
def nbUsers
pp 'debug: nbUsersCalled'
@count
end
And my javascript :
export default class OnSamePageSubscription extends React.Component {
constructor(props) {
super(props);
this.cable = ActionCable.createConsumer(WEB_SOCKET_HOST);
this.subscribe = () => {
this.channel = this.cable.subscriptions.create(
{
channel: 'OnTheSamePageChannel',
url: props.url,
current_user: props.current_user
},
{
connected: this.connected,
disconnected: this.disconnected,
received: this.received,
rejected: this.rejected
}
);
};
this.unsubscribe = () => {
this.channel.unsubscribe()
};
}
received = (data) => {
console.log(`Connected user : ${data.user}`);
this.props.onUpdate(data);
};
connected = () => {
console.log(`Tracking connection`);
};
disconnected = () => {
console.warn(`Tracking disconnected.`);
};
rejected = () => {
console.warn('Tracking rejected');
};
}
The question is : how to call this method (nbUsers) and getting result ?
I tried https://robots.thoughtbot.com/talking-to-actioncable-without-rails but this is not working