vue-echo-laravel
Version:
It's a laravel/echo wrapper for Vue JS ^2.0. This Vue plugin injects a Laravel Echo instance into all of your vue instances, allowing for a simple channel subscription on each instance, or using Laravel Echo through this.$echo. It will be synced with late
67 lines (58 loc) • 1.98 kB
JavaScript
import Echo from 'laravel-echo';
export default {
install(Vue, options) {
if (!options) {
throw new Error("[Vue-Echo] cannot locate options");
}
if (typeof options !== 'object') {
throw new Error("[Vue-Echo] cannot initiate options");
}
if(typeof options.socketId == 'function')
{
Vue.prototype.$echo = options;
}
else
{
Vue.prototype.$echo = new Echo(options);
}
Vue.mixin({
created(){
let channel = this.$options['channel'];
if (typeof channel === 'function')
{
channel = channel.bind(this).call();
}
if(channel)
{
if(channel.startsWith('private:'))
{
this.channel = this.$echo.private(channel.replace('private:', ''))
}
else if(channel.startsWith('presence:'))
{
this.channel = this.$echo.join(channel.replace('presence:', ''))
}
else
{
this.channel = this.$echo.channel(channel);
}
let events = this.$options['echo'];
if(events)
{
Object.keys(events).forEach(function(key){
// Bind the VM as second parameter
this.channel.listen(key, (payload) => {
events[key].bind(this)(payload);
});
}, this);
}
}
},
beforeDestroy(){
if(this.$options['channel']){
this.channel.unsubscribe();
}
}
})
}
}