[Solved] Accessing this.$socket into a Vuex action.js file ?



  • Hi,
    I’m using vue-socket.io-extend to manage websockets.
    I would like to trigger a socket.emit() from within a Vuex actions.js file but this throws an error:
    TypeError: Cannot read property 'emit' of undefined

    Should I have to import anythig?

    Here is my action.js file

    export const saveUserLog = ({ commit, rootState }, data) => {
      data.user = rootState.globalModule.userConnected._id
      commit('addUserLogToTempMemory', data)
      this.$socket.emit('saveUserLog', data) // throws an error
    }
    
    Thanks for help !


  • I finally found the solution by myself in this topic

    import Vue from 'vue' // importing Vue
    
    export const saveUserLog = ({ commit, rootState }, data) => {
      data.user = rootState.globalModule.userConnected._id
      commit('addUserLogToTempMemory', data)
      Vue.$socket.emit('saveUserLog', data) // Using Vue
    }
    


  • What does your config look like? I want to add socketio to my Quasar app, but after installing socketio via npm and adding it to my project via quasar new plugin socketio, I still can’t get it to work as a global plugin. Would like to know the proper way to add plugins that actually works.



  • same for me, it is driving me crazy


Log in to reply