npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-action-cable

v1.1.0

Published

A simple Vue.js wrapper for Rails Action Cable.

Downloads

5

Readme

Vue Action Cable

A simple Vue.js wrapper for Rails Action Cable.

Installation

npm install vue-action-cable

Then use it in Vue.js:

import VueActionCable from 'vue-action-cable'

Vue.use(VueActionCable, {
    connectionUrl: 'wss://www.your-app.de/cable'
})

You might want to use something like process.env.ACTION_CABLE_URL as connectionUrl in order to satisfy development and production environments.

Usage

This package simply exposes $cable on your Vue instance which is nothing other than a wrapper on Rails Action Cable's createConsumer method. Use it something like this:

mounted () {
    // This ensures a single web socket connection. Call it in every component that wants to use Action Cable.
    this.websocket = this.$cable.useGlobalConnection()

    // Or with an auth token: Calls your connectionUrl with a token GET-param, e.g. 'wss://www.your-app.de/cable?token=1290e42e42e5e1d68220d8de66092e75'
    const token = this.$auth.getToken('local').replace('Bearer ', '')
    this.websocket = this.$cable.useGlobalConnection(token)
}

And then access Action Cable's methods like so:

this.subscription = this.websocket.subscriptions.create({ channel: 'AppearancesChannel', user_id: this.user.id }, {
    connected: () => console.log('Connected to AppearancesChannel for user #' +  this.user.id),
    received: (data) => {
        const message = JSON.parse(data)
        this.user.is_online = message
    }
})
// or directly like so as 'useGlobalConnection() only returns $cable.connection':
this.subscription = this.$cable.connection.subscriptions.create(...

Don't forget to cleanup to avoid duplicate subscriptions and angry users:

destroyed () {
    if (this.subscription) {
        this.subscription.unsubscribe()
    }
}

Manual Connection

If you want to use multiple websocket connections on the same page, simply call the connect method yourself:

this.websocket = this.$cable.connect()

// or with an auth token
this.$cable.connect('1290e42e42e5e1d68220d8de66092e75')

Close Websocket Connection

// Global existing connection
this.websocket = this.$cable.useGlobalConnection()
this.$cable.disconnect()

// Manually created ones
this.websocket = this.$cable.connect()
this.websocket.disconnect()