38 lines
813 B
JavaScript
38 lines
813 B
JavaScript
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: () => {
|
|
console.log('Connected to ChatRoom ' + chatroomId)
|
|
},
|
|
received: data => {
|
|
console.log(data)
|
|
}
|
|
})
|
|
|
|
// Testing ActionCable
|
|
const testBtn = document.getElementById('test-btn')
|
|
testBtn.addEventListener('click', event => {
|
|
fetch(`/chat_rooms/${chatroomId}/cable_testing` , {
|
|
method: 'POST',
|
|
body: JSON.stringify({})
|
|
})
|
|
})
|
|
|
|
|
|
|