Installations
npm install actioncable-vue
Score
80.6
Supply Chain
100
Quality
79.9
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Developer
mclintprojects
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
21.5.0
NPM Version
10.8.3
Statistics
182 Stars
284 Commits
37 Forks
4 Watching
2 Branches
10 Contributors
Updated on 16 Nov 2024
Bundle Size
20.48 kB
Minified
5.77 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
1,624,032
Last day
-13.9%
945
Compared to previous day
Last week
2.6%
5,826
Compared to previous week
Last month
9.3%
24,602
Compared to previous month
Last year
-2.5%
366,939
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
ActionCableVue is an easy-to-use Action Cable integration for VueJS.
🚀 Installation
1npm install actioncable-vue --save
1// Vue 2.x 2import Vue from "vue"; 3import ActionCableVue from "actioncable-vue"; 4import App from "./App.vue"; 5 6Vue.use(ActionCableVue, { 7 debug: true, 8 debugLevel: "error", 9 connectionUrl: "ws://localhost:5000/api/cable", // or function which returns a string with your JWT appended to your server URL as a query parameter 10 connectImmediately: true, 11}); 12 13new Vue({ 14 router, 15 store, 16 render: (h) => h(App), 17}).$mount("#app");
1// Vue 3.x 2import { createApp } from "vue"; 3import App from "./App.vue"; 4import ActionCableVue from "actioncable-vue"; 5 6const actionCableVueOptions = { 7 debug: true, 8 debugLevel: "error", 9 connectionUrl: "ws://localhost:5000/api/cable", 10 connectImmediately: true, 11 unsubscribeOnUnmount: true, 12}; 13 14createApp(App) 15 .use(store) 16 .use(router) 17 .use(ActionCableVue, actionCableVueOptions) 18 .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/Function | null | Optional | ActionCable websocket server url. Omit it for the default behavior |
connectImmediately | Boolean | true | Optional | ActionCable connects to your server immediately. If false, ActionCable connects on the first subscription. |
unsubscribeOnUnmount | Boolean | true | Optional | Unsubscribe from channels when component is unmounted. |
store | Object | null | Optional | Vuex store |
Table of content
- Support ActionCable-Vue
- Component Level Usage
- Subscriptions
- Unsubscriptions
- Manually connect to the server
- Disconnecting from the server
- Performing an action on your Action Cable server
- Usage with Vuex
- Usage with Nuxt.JS
Wall of Appreciation
- Many thanks to @x88BitRain for adding Vue 3 compatibility
☕ Support ActionCable-Vue
If you'd like to donate to support the continued development and maintenance of actioncable-vue, you can do so here.
🌈 Component Level Usage
If you want to listen to channel events from your Vue component:
- You need to either add a
channels
object in the Vue component - If you're using
vue-class-component
define achannels
property. - If you're using Vue 3
defineComponent
define achannels
property.
Each defined object in channels
will start to receive events provided you subscribe correctly.
1new Vue({ 2 data() { 3 return { 4 message: "Hello world", 5 }; 6 }, 7 channels: { 8 ChatChannel: { 9 connected() {}, 10 rejected() {}, 11 received(data) {}, 12 disconnected() {}, 13 }, 14 }, 15 methods: { 16 sendMessage: function () { 17 this.$cable.perform({ 18 channel: "ChatChannel", 19 action: "send_message", 20 data: { 21 content: this.message, 22 }, 23 }); 24 }, 25 }, 26 mounted() { 27 this.$cable.subscribe({ 28 channel: "ChatChannel", 29 room: "public", 30 }); 31 }, 32});
Alternative definition for vue-class-component
users.
1@Component 2export default class ChatComponent extends Vue { 3 @Prop({ required: true }) private id!: string; 4 5 get channels() { 6 return { 7 ChatChannel: { 8 connected() { 9 console.log("connected"); 10 }, 11 rejected() {}, 12 received(data) {}, 13 disconnected() {}, 14 }, 15 }; 16 } 17 18 sendMessage() { 19 this.$cable.perform({ 20 channel: "ChatChannel", 21 action: "send_message", 22 data: { 23 content: this.message, 24 }, 25 }); 26 } 27 28 async mounted() { 29 this.$cable.subscribe({ 30 channel: "ChatChannel", 31 room: "public", 32 }); 33 } 34}
Alternative definition for Vue 3 defineComponent
users.
1import { onMounted } from 'vue'; 2 3export default defineComponent({ 4 channels: { 5 ChatChannel: { 6 connected() { 7 console.log('connected'); 8 }, 9 rejected() { 10 console.log('rejected'); 11 }, 12 received(data) {}, 13 disconnected() {}, 14 }, 15 }, 16 setup() { 17 onMounted(() => { 18 this.$cable.subscribe({ 19 channel: "ChatChannel", 20 }); 21 }); 22 }, 23});
✨ Subscriptions
1. Subscribing to a channel
Define a channels
object in your component matching the action cable server channel name you passed for the subscription.
1new Vue({ 2 channels: { 3 ChatChannel: { 4 connected() { 5 console.log("I am connected."); 6 }, 7 }, 8 }, 9 mounted() { 10 this.$cable.subscribe({ 11 channel: "ChatChannel", 12 }); 13 }, 14});
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.
2. Subscribing to the same channel but different rooms
1new Vue({ 2 channels: { 3 chat_channel_public: { 4 connected() { 5 console.log("I am connected to the public chat channel."); 6 }, 7 }, 8 chat_channel_private: { 9 connected() { 10 console.log("I am connected to the private chat channel."); 11 }, 12 }, 13 }, 14 mounted() { 15 this.$cable.subscribe( 16 { 17 channel: "ChatChannel", 18 room: "public", 19 }, 20 "chat_channel_public", 21 ); 22 this.$cable.subscribe( 23 { 24 channel: "ChatChannel", 25 room: "private", 26 }, 27 "chat_channel_private", 28 ); 29 }, 30});
3. Subscribing to a channel with a computed name
1// Conversations.vue 2 3new Vue({ 4 methods: { 5 openConversation(conversationId){ 6 this.$router.push({name: 'conversation', params: {id: conversationId}); 7 } 8 } 9});
1// Chat.vue 2 3new Vue({ 4 channels: { 5 computed: [ 6 { 7 channelName() { 8 return `${this.$route.params.conversationId}`; 9 }, 10 connected() { 11 console.log("I am connected to a channel with a computed name."); 12 }, 13 rejected() {}, 14 received(data) {}, 15 disconnected() {}, 16 }, 17 ], 18 }, 19 mounted() { 20 this.$cable.subscribe({ 21 channel: this.$route.params.conversationId, 22 }); 23 }, 24});
🎃 Unsubscriptions
When your component is destroyed ActionCableVue automatically unsubscribes from any channel that component was subscribed to.
1new Vue({ 2 methods: { 3 unsubscribe() { 4 this.$cable.unsubscribe("ChatChannel"); 5 }, 6 }, 7 mounted() { 8 this.$cable.subscribe({ 9 channel: "ChatChannel", 10 }); 11 }, 12});
👺 Manually connect to the server
ActionCableVue automatically connects to your Action Cable server if connectImmediately
is not set to false
during setup. If you do set connectImmediately
to false
you can manually trigger a connection to your ActionCable server with this.$cable.connection.connect
.
1new Vue({ 2 methods: { 3 connectWithRefreshedToken(token) { 4 // You can optionally pass in a connection URL string 5 // You can optionally pass in a function that returns a connection URL 6 // You can choose not to pass in anything and it'll reconnect with the connection URL provided during setup. 7 this.$cable.connection.connect( 8 `ws://localhost:5000/api/cable?token=${token}`, 9 ); 10 }, 11 }, 12});
👽 Disconnecting from the server
1new Vue({ 2 methods: { 3 disconnect() { 4 this.$cable.connection.disconnect(); 5 }, 6 }, 7});
💎 Performing an action on your Action Cable server
Requires that you have a method defined in your Rails Action Cable channel whose name matches the action property passed in.
1new Vue({ 2 channels: { 3 ChatChannel: { 4 connected() { 5 console.log("Connected to the chat channel"); 6 }, 7 received(data) { 8 console.log("Message received"); 9 }, 10 }, 11 }, 12 methods: { 13 sendMessage() { 14 this.$cable.perform({ 15 channel: "ChatChannel", 16 action: "send_message", 17 data: { 18 content: "Hi", 19 }, 20 }); 21 }, 22 }, 23 mounted() { 24 this.$cable.subscribe({ 25 channel: "ChatChannel", 26 }); 27 }, 28});
🐬 Usage with Vuex
ActionCableVue has support for Vuex. All you have to do is setup your store correctly and pass it in during the ActionCableVue plugin setup.
1// store.js 2 3import Vue from "vue"; 4import Vuex from "vuex"; 5 6Vue.use(Vuex); 7 8export default new Vuex.Store({ 9 state: {}, 10 mutations: { 11 sendMessage(state, content) { 12 this.$cable.perform({ 13 action: "send_message", 14 data: { 15 content, 16 }, 17 }); 18 }, 19 }, 20 actions: { 21 sendMessage({ commit }, content) { 22 commit("sendMessage", content); 23 }, 24 }, 25});
1import store from "./store"; 2import Vue from "vue"; 3import ActionCableVue from "actioncable-vue"; 4 5Vue.use(ActionCableVue, { 6 debug: true, 7 debugLevel: "all", 8 connectionUrl: process.env.WEBSOCKET_HOST, 9 connectImmediately: true, 10 store, 11});
💪 Usage with Nuxt.JS
ActionCableVue works just fine with Nuxt.JS. All you need to do is set it up as a client side plugin.
1// /plugins/actioncable-vue.js 2 3import Vue from "vue"; 4import ActionCableVue from "actioncable-vue"; 5 6if (process.client) { 7 Vue.use(ActionCableVue, { 8 debug: true, 9 debugLevel: "all", 10 connectionUrl: process.env.WEBSOCKET_HOST, 11 connectImmediately: true, 12 }); 13}
Don't forget to register your plugin.
1// nuxt.config.js 2/* 3 ** Plugins to load before mounting the App 4 */ 5plugins: [{ src: "@/plugins/actioncable-vue", ssr: false }];
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
30 commit(s) and 8 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
Found 0/12 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/mclintprojects/actioncable-vue/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/mclintprojects/actioncable-vue/test.yml/master?enable=pin
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
4.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to actioncable-vue
@types/rails__actioncable
TypeScript definitions for @rails/actioncable
@types/actioncable
TypeScript definitions for actioncable
@kesha-antonov/react-native-action-cable
Use Rails 5+ ActionCable channels with React Native for realtime magic.
react-native-actioncable
ActionCable for react native