Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to execute a method on rails via action cable
socket = new WebSocket('ws://localhost:3000/api/v1/action_cable');
const onMessage = (ws, store) => evt => {
let msg = JSON.parse(evt.data);
if (msg.type === "ping") {
return;
}
console.log("FROM RAILS: ", msg);
}
socket.onmessage = onMessage(socket);
// you need to subscribe to the channel first
var msg = {
command: 'subscribe',
identifier: JSON.stringify({
channel: 'ReadingsChannel',
}),
};
socket.send(JSON.stringify(msg));
// the method foobar is going to be executed on Rails (channel file)
var msg = {
command: 'message',
identifier: JSON.stringify({
channel: 'ReadingsChannel',
}),
data: JSON.stringify({
action: 'foobar',
code: 1234
}),
};
socket.send(JSON.stringify(msg));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.