rails-action-cable
v1.0.3
Published
NPM package for rails action cable client
Downloads
3
Readme
N.B: This is a direct extract from rails cable.js to use as a node module. This is experimental, Do not use!!!.
Example Usage
const ActionCable = require('rails-action-cable');
const App = {};
App.cable = ActionCable.createConsumer('ws://***.com/cable?jwt_credentials=***');
App.cable.subscriptions.create(
{ channel: "MessagesChannel", conversation_id: 1 },
{
received: function(data){
console.log(data);
},
connected: function(){
console.log('Now connected');
},
disconnected: function(){
console.log('Now disconnected');
}
}
)
Description
ActionCable.createConsumer
- This connects to the specified websocket url. Here, you must specify jwt_credentials
for it to authenticate against the backend otherwise, you will just be rejected.
App.cable.subscriptions.create
- This subscribes to a specific channel. Always remember to pass in the conversation_id
as this helps the client connect to the right channel and receive accurate messages.
This is all! No need to send any message via websockets once you have been connected to the websocket backend and have subscribed to the right channel. Everyother thing should be done via the graphql mutations which on message creation broadcasts new messages to the right channel based on the details found in those messages. Thanks!!!