M
M
Maxim2020-01-31 13:02:40
Vue.js
Maxim, 2020-01-31 13:02:40

How to exit laravel + Vue broadcast channel?

where am i doing it wrong?

when I select a dialog, I subscribe to the chat.{id dialoga} channel, the chat

is going on, the broadcast is working,
5e33fb1d028b7793643187.png
5e33fb248d589074430084.png

but when I go to the favorite dialogs tab, the dialogsFavorites() method then I exit the chat.{id of the previous dialoga} channel, I call the destroyChannel() method,

but messages come all smoothly, even when the dialogue is not selected
5e33fb12da022723956796.png

export default {
    name: "Dialogs",
    created(){
    },
    watch: {
    },
    computed: {
        channel(){
            return  window.Echo.private('chat.' + this.dialogSelect);
        },
    },
    mounted() {
        this.User();
        this.Dialogs();
    },
    methods: {
        addChanel(){
            console.log('chat.' + this.dialogSelect);
            if (this.dialogSelect > 0){

                this.channel
                    .listen('DialogMessage',({data}) => {
                        this.messages.push(data);
                        this.isTyping = false;
                    })
                    .listenForWhisper('typing', (e) => {
                        console.log(e);
                        this.isTyping = e;

                        if (this.typingTimer) clearTimeout(this.typingTimer);

                        this.typingTimer = setTimeout(() => {
                            this.isTyping = false;
                        }, 2000);
                    });
            }

        },
        sendMessage(){
            axios.post('/profile/dialogs/send',{
                message:this.message,
                chat_id: this.dialogSelect,
                user_id: this.userSelect.id,
                type: 1
            })
                .then((response) => {
                    this.message = '';
                })
                .catch(error => {});
        },
        destroyChannel(){
            window.Echo.leaveChannel('chat.' + this.dialogSelect);
        },
        selectUser(id,user,x,avatar, fullname){
          this.destroyChannel();
          this.dialogSelect  = id;
          this.userSelect.id  = user;
          this.userSelect.avatar  = avatar;
          this.userSelect.fullname  = fullname;
          this.dialogIndex  = x;

            this.addChanel();

            axios.post('/profile/dialogs/messages',{
                dialog_id: id,
                offset: this.offset,
                limit: this.limit
            })
                .then((response) => {
                    this.messages = response.data.messages;
                    this.dialog.like = response.data.like;
                    this.dialog.favorite = response.data.favorite;
                    this.dialog.ignore = response.data.ignore;
                })
                .catch(error => {});
        },
        dialogFavorites(){
            this.destroyChannel();
            this.dialogNone();
            axios.get('/profile/dialogs/favorites')
                .then((response) => {
                    this.dialogs = response.data;
                    this.activeFilter = 'f';
                })
                .catch(error => {});
        },
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2020-01-31
@Aslero

Maxim , I already wrote to you, look at the documentation , it also says about private channels that you should use not .leaveChannel(), but .leave() then there will be a complete unsubscribe from any broadcast.

If you want to leave a channel and its associated private and presence channels, you can call the exit method:
Echo.leave('orders');

Also try to see what the method unsubscribes you from
destroyChannel(){
    console.log('chat.' + this.dialogSelect)
    window.Echo.leaveChannel('chat.' + this.dialogSelect);
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question