MetinSeylan / Vue-Socket.io

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

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

Open JH-Eric-Yang opened 1 year ago

JH-Eric-Yang commented 1 year 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.