A pusher plugin for vue.js
In my personal projects, I kinda needed the pusher instance across all components without having to re-initialize since it's a bit of a maintenance issue.
Install the plugin like any other plugin. :)
var Vue = require('vue');
Vue.use(require('vue-pusher'), {
api_key: 'xxxxxx',
options: {
cluster: 'ap1',
encrypted: true,
}
});
Inside your components, you just need to access the $pusher
object.
export default {
ready () {
var channel = this.$pusher.subscribe('dashboard');
channel.bind('user.log', ({ log }) => {
console.log(`User ${log.user.name} has ${log.action} at ${log.time}`);
});
}
}
Subscribing to a channel providers a callback where you can bind events.
this.$pusher.subscribe('dashboard', channel => {
channel.bind('user.log', ({ log }) => {
console.log(`User ${log.user.name} has ${log.action} at ${log.time}`);
});
});
this.$pusher.unsubscribe('dashboard');