[Vuejs]-Best approach of Socket.io in Vue applications

0👍

You could have a start action in your vuex, and in that action you could listen the events.

Eg.

//...vuex module

// onChange is a normal function that is not inside: actions or mutations, but is declared in the samefile for ease of use
const onChange = ({commit}) => (...eventParams)=>{
  commit("...", changes)
}

// Inside actions
async start({commit}){
  socket.on(event, onChange({commit}))
  //...you could repeat the pattern for any mutation
}

Leave a comment