Answer the question
In order to leave comments, you need to log in
How to work correctly in vuex with websocket?
Hello. There are several dozen websocket connections, the data from which needs to be processed and used in different application components. How it is better to implement it? It seems logical through vuex, but it’s not clear how to put them there correctly, and then update them.
Thanks for any ideas.
Answer the question
In order to leave comments, you need to log in
You create a store in it with the necessary mutation and action. Where you have updated data on sockets, you use dispatch and pass the data as the second argument. You can use some plugin to work with sockets, there are a lot of options. And what is right for your current project, no one will say without understanding it first
Choose a library for sockets, put the connection in the action. When updating data, make a commit to the desired mutations.
Something like this:
export default {
...
actions: {
connectToChat({commit}, payload){
socketLib.connect('users', d => {
commit('updateUsers', d);
});
}
}
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question