polyglot/app/javascript/packs/chatrooms.js

37 lines
758 B
JavaScript
Raw Normal View History

import ActionCable from 'actioncable'
// create App object with key cable == new cosumer
(function() {
window.App || (window.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
// find chatroom id
const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"]
// create subsciptions
App['chatroom' + chatroomId] = App.cable.subscriptions.create({
channel: 'ChatRoomsChannel',
room: chatroomId
}, {
connected: () => {
},
received: data => {
2018-09-04 10:49:18 +00:00
console.log(data)
}
})
// Testing ActionCable
2018-09-04 10:49:18 +00:00
const testBtn = document.getElementById('test-btn')
testBtn.addEventListener('click', event => {
fetch(`/chat_rooms/${chatroomId}/cable_testing` , {
method: 'POST',
body: JSON.stringify({})
})
})