Package Exports
- actioncable-vue
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (actioncable-vue) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ActionCableVue is an easy-to-use Action Cable integration for VueJS.
🚀 Installation
npm install actioncable-vue --save
import Vue from 'vue';
import App from './App.vue';
import ActionCableVue from 'actioncable-vue';
Vue.use(ActionCableVue, {
debug: true,
debugLevel: 'error',
connectionUrl: 'ws://localhost:5000/api/cable'
});
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app');
Parameters | Type | Default | Required | Description |
---|---|---|---|---|
debug | Boolean | false |
Optional | Enable logging for debug |
debugLevel | String | error |
Optional | Debug level required for logging. Either info , error , or all |
connectionUrl | String | null |
Required | ActionCable websocket server url |
🌈 Component Level Usage
If you want to listen socket events from component side, you need to add `sockets` object in Vue component, and every function will start to listen events, depends on object key
new Vue({
data() {
return {
message: 'Hello world'
};
},
channels: {
ChatChannel: {
connected() {},
rejected() {},
received(data) {},
disconnected() {}
}
},
methods: {
sendMessage: function() {
this.$cable.perform('ChatChannel', 'send_message', {
content: this.message
});
}
},
mounted() {
this.$cable.subscribe({ channel: 'ChatChannel', room: 'public' });
}
});
Subscriptions
1. Subscribing to a channel
Requires that you have an object defined in your channels
matching the action cable server channel name you passed for the subscription.
new Vue({
channels: {
ChatChannel: {
connected() {
console.log('I am connected.');
}
}
},
mounted() {
this.$cable.subscribe({ channel: 'ChatChannel' });
}
});
2. Subscribing to the same channel but multiple rooms
new Vue({
channels: {
'chat_channel_public': {
connected() {
console.log('I am connected to the public chat channel.');
}
},
'chat_channel_private': {
connected() {
console.log('I am connected to the private chat channel.');
}
}
},
mounted() {
this.$cable.subscribe({ channel: 'ChatChannel', room: 'public', 'chat_channel_public' });
this.$cable.subscribe({ channel: 'ChatChannel', 'private' }, 'chat_channel_private');
}
});
Important
ActionCableVue automatically uses your ActionCable server channel name if you do not pass in a specific channel name to use in your
channels
. It will also override clashing channel names.