MetinSeylan / Vue-Socket.io

😻 Socket.io implementation for Vuejs and Vuex
https://metin.sh
MIT License
3.95k stars 499 forks source link

New socket instance created if vuex’s store mutation function called inside socket #338

Open JH-Eric-Yang opened 2 years ago

JH-Eric-Yang commented 2 years ago

Hi, I tired to call a mutation function in the vuex's store inside a socket event listener. It looks something like this

import { createStore } from "vuex";

export default createStore({
    state() {
        return {
            message: "default message",
        };
    },
    mutations: {
        SOCKET_updateMessage(state, message) {
            console.log("SOCKET_updateMessage: message:", message);
            state.message = message;
            this.updateMessage(message);
        },

     updateMessage(message) {
            console.log(message)
            },
    },
});

It will work but I notice that it will recreate a new socket instance. The client will create and communicate with the server with the new socket instance (with different socket.id). I wonder why this is happening. Thank you.