GithubHelp home page GithubHelp logo

vue-echo's Introduction

vue-echo

Vue 2 integration for the Laravel Echo library with latest dependencies.

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. You can use socket.io and pusher or any config as per laravel/echo

This project does not require updates as it directly uses your latest version

NPM LINK package/vue-echo-laravel

Install

via yarn

yarn add vue-echo-laravel

or via npm

npm install vue-echo-laravel --save

Usage

Initialize

First you'll need to register the plugin and, optionally, initialize the Echo instance.

import VueEcho from 'vue-echo-laravel';
  
Vue.use(VueEcho, {
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001',
});

/**
 * Alternatively you can pass an echo instance:
 * ********************************************
 * import Echo from 'laravel-echo';
 * 
 * const EchoInstance = new Echo({
 *  broadcaster: 'socket.io',
 *   host: window.location.hostname + ':6001',
 *   namespace: 'App.Events',
 * });
 * Vue.use(VueEcho, EchoInstance);
 */

Using Echo

Once vue-echo is registered, every vue instance is able to subscribe to channels and listen to events through the this.$echo property on the connection you specified earlier.

var vm = new Vue({
    mounted() {
        // Listen for the 'NewBlogPost' event in the 'team.1' private channel
        this.$echo.private('team.1').listen('NewBlogPost', (payload) => {
            console.log(payload);
        });
    }
});

Subscribe your Vue instance to a single channel

You can subscribe a vue instance to a single standard channel if needed and define your events.

var vm = new Vue({
    channel: 'blog',
    echo: {
        'BlogPostCreated': (payload) => {
            console.log('blog post created', payload);
        },
        'BlogPostDeleted': (payload) => {
            console.log('blog post deleted', payload);
        }
    }
});

You can feel free to use this inside Your methods.

Subscribing to channels

Laravel Echo allows you to subscribe to: normal, private and presence channels.

In the example above, we subscribed to a standard channel.

Private channel

If you would like to subscribe to a private channel instead, prefix your channel name with private:

var vm = new Vue({
    channel: 'private:user.1',
    echo: {
        'BlogPostCreated': (payload) => {
            console.log('blog post created', payload);
        },
        'BlogPostDeleted': (payload) => {
            console.log('blog post deleted', payload);
        }
    }
});

If you need to compute channel name, you should pass channel as function.

var vm = new Vue({
    channel() {
        return `private:user.${ this.userId }`
    },
    echo: {
        'BlogPostCreated': (payload) => {
            console.log('blog post created', payload);
        },
        'BlogPostDeleted': (payload) => {
            console.log('blog post deleted', payload);
        }
    },

    computed: {
        userId() {
            return 1;
        }
    }
});
Presence channel

If you would like to subscribe to presence channel instead, prefix your channel name with presence:

var vm = new Vue({
    channel: 'presence:user.1.chat',
    echo: {
        'NewMessage': (payload) => {
            console.log('new message from team', payload);
        }
    }
});

Manually listening to events

If there's a scenario where you want to listen to events after certain conditions are met, you can do so through this.channel:

var vm = new Vue({
    channel: 'private:user.1',
    echo: {
        'BlogPostCreated': (payload) => {
            console.log('blog post created', payload);
        },
        'BlogPostDeleted': (payload) => {
            console.log('blog post deleted', payload);
        }
    },
    mounted(){
        if(window.user.role == 'admin'){
            this.channel.listen('BlogPostEdited', (payload) => {
                console.log('As admin I get notified of edits', payload);
            });
        }
    }
});

Acepting PR for Vue Composition API

vue-echo's People

Contributors

k0mar12 avatar msonowal avatar strangergithuber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue-echo's Issues

types for library

I'm using a typescript vitejs generated project. Is there a chance to have a .d.ts files?

Could not find a declaration file for module 'vue-echo-laravel'. '.../test-pusher/node_modules/vue-echo-laravel/dist/build.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/vue-echo-laravel` if it exists or add a new declaration (.d.ts) file containing `declare module 'vue-echo-laravel';`ts(7016)

Can't connect socket

Hi,

I really appreciate this awesome plugin but I couldn't connect to Laravel. Apparently the websocket connection is ok but when I dispatch events from the server I never can catch them in my client. I am working in a separate VUE-CLI SPA app.

I attached some images if you know why the socket is always disconnected

Captura de pantalla 2021-03-05 a las 14 47 27

Captura de pantalla 2021-03-05 a las 14 47 52

.

Thanks!

withCredential

Hi!
thanks for this package, I will start using it in my vue spa.
The only thing that is not working is the authEndpoint, because I cannot set cookies, so there is no session.
Is there a solution to this?
Thanks!

dhcmega

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.