Gathering detailed insights and metrics for actioncable-vue
Gathering detailed insights and metrics for actioncable-vue
Gathering detailed insights and metrics for actioncable-vue
Gathering detailed insights and metrics for 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
npm install actioncable-vue
80.6
Supply Chain
100
Quality
79.9
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
182 Stars
284 Commits
37 Forks
4 Watching
2 Branches
10 Contributors
Updated on 16 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
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
ActionCableVue is an easy-to-use Action Cable integration for VueJS.
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 |
If you'd like to donate to support the continued development and maintenance of actioncable-vue, you can do so here.
If you want to listen to channel events from your Vue component:
channels
object in the Vue componentvue-class-component
define a channels
property.defineComponent
define a channels
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});
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});
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.
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});
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});
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});
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});
1new Vue({ 2 methods: { 3 disconnect() { 4 this.$cable.connection.disconnect(); 5 }, 6 }, 7});
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});
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});
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
Reason
1 existing vulnerabilities detected
Details
Reason
Found 0/12 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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 More