Vue.js integration for Laravel Echo
Vue-Echo provides a plugin for integrating Laravel Echo, a JavaScript library for real-time event broadcasting, into Vue.js applications. This package is specifically designed for Vue 2 applications, injecting an Echo instance into all Vue components via `this.$echo` and offering declarative channel subscription options within component definitions. The current stable version is 1.0.2, with its last known update fixing a minor bug. The package's release cadence appears to have slowed considerably, likely due to its focus on Vue 2 while Vue 3 is the current major version. It differentiates itself by simplifying the integration of Laravel Echo's real-time features with Vue's component lifecycle and reactive data patterns, offering both global and component-specific event handling mechanisms.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'private') at VueComponent.mounted
cause The `vue-echo` plugin or `laravel-echo` instance was not properly initialized or registered with Vue.fixEnsure `Vue.use(VueEcho, { broadcaster: 'pusher', key: '...' });` is called before creating your root Vue instance, and that all necessary `laravel-echo` dependencies (like `pusher-js` or `socket.io-client`) are installed and configured correctly. -
TypeError: this.$echo is undefined
cause The `vue-echo` plugin has not been registered with Vue, or the component is being accessed before the plugin has fully initialized.fixVerify that `Vue.use(VueEcho, ...)` is invoked correctly and that `vue-echo` and `laravel-echo` are installed (`npm install vue-echo laravel-echo pusher-js`). For server-side rendering (SSR), ensure `this.$echo` access is guarded against non-browser environments. -
Webpack compilation error: Cannot find module 'vue-echo'
cause The `vue-echo` package is not installed as a project dependency.fixRun `npm install vue-echo laravel-echo --save` or `yarn add vue-echo laravel-echo` to add the package to your project.
Warnings
- breaking Version 1.0.0 introduced `vm` as a second parameter to locally registered event callbacks (i.e., callbacks defined in the `echo` option of a Vue component). This was added to provide direct access to the Vue instance, improving context handling. Code written for versions prior to 1.0.0 where `this` was used within these callbacks might need adjustment if `this` was expected to refer to the Vue instance.
- gotcha This library is designed for Vue 2. It will not work with Vue 3 due to fundamental changes in Vue's plugin system and reactivity model. Attempting to use `vue-echo` in a Vue 3 project will result in errors.
- gotcha When defining event listeners within the `echo` option of a Vue component, the `this` context inside the callback does not directly refer to the Vue instance by default (prior to v1.0.0, or if not using the `vm` parameter in v1.0.0+).
- gotcha The `channel` option on a Vue instance (`this.channel`) only references the instance's specifically subscribed channel when using the declarative `channel` option. For other channels, use the global `this.$echo` instance.
Install
-
npm install vue-echo -
yarn add vue-echo -
pnpm add vue-echo
Imports
- VueEcho
import { VueEcho } from 'vue-echo';import VueEcho from 'vue-echo';
- $echo
this.$echo.private('channel').listen('Event', callback); - channel/echo component options
new Vue({ channel: 'my-channel', echo: { 'MyEvent': (payload, vm) => { /* ... */ } } });
Quickstart
import Vue from 'vue';
import VueEcho from 'vue-echo';
import Echo from 'laravel-echo'; // Assuming laravel-echo is installed
// Option 1: Initialize VueEcho directly with configuration
Vue.use(VueEcho, {
broadcaster: 'pusher',
key: 'YOUR_PUSHER_APP_KEY', // Replace with your Pusher App Key or environment variable
cluster: 'mt1', // Optional, replace with your cluster
forceTLS: true // Optional
});
// Option 2: Pass an existing Laravel Echo instance
/*
const echoInstance = new Echo({
broadcaster: 'pusher',
key: 'YOUR_PUSHER_APP_KEY',
cluster: 'mt1',
forceTLS: true
});
Vue.use(VueEcho, echoInstance);
*/
const app = new Vue({
el: '#app',
data: {
messages: []
},
// Declarative channel subscription within the component
channel: 'public.chat',
echo: {
'ChatMessageSent': (payload, vm) => {
console.log('New message received via declarative config:', payload.message);
vm.messages.push(payload.message);
}
},
mounted() {
console.log('VueEcho instance available:', this.$echo);
// Manual subscription via this.$echo
this.$echo.private('private.notifications.user.1')
.listen('NewNotification', (notification) => {
console.log('New private notification:', notification);
});
// Manually listen on the component's channel if defined
if (this.channel) {
this.channel.listen('AnotherEvent', (payload) => {
console.log('Another event on component channel:', payload);
});
}
},
template: `
<div>
<h1>Vue-Echo Example</h1>
<p>Check console for real-time events.</p>
<h2>Public Chat Messages:</h2>
<ul>
<li v-for="(msg, index) in messages" :key="index">{{ msg }}</li>
</ul>
</div>
`
});