user1050817
user1050817

Reputation: 965

Nuxt: Vuex commit or dispatch message outside vuejs component

I have an application in nuxt that I want to connect to a websocket, I have seen examples where the callback to receive messages is placed inside a component, but I do not think ideal, I would like to place the callback inside my store, currently my code is something like this

//I'm using phoenix websocket
var ROOT_SOCKET = `wss://${URL}/socket`;

var  socket = new Socket(ROOT_SOCKET);
socket.connect()
var chan = socket.channel(`connect:${guid}`); 

chan.join();
console.log("esperando mensj");

chan.on("translate", payload => {
  console.log(JSON.stringify(payload));
  <store>.commit("loadTranslation",payload) //<- how can I access to my store?
})

chan.onError(err => console.log(`ERROR connecting!!! ${err}`));


const createStore = () => {
  return new Vuex.Store({
   state: {},

   mutations:{
    loadTranslation(state,payload){...}
   },


....
})}

how can I access to my store inside my own store file and make a commit??? is it possible?... I know there is a vuex plugin but I can't really understand well the documentation and I'll prefer build this without that plugin https://vuex.vuejs.org/guide/plugins.html

thank you guys...hope you can help me...

Upvotes: 0

Views: 2099

Answers (1)

Aldarund
Aldarund

Reputation: 17621

You can do it in nuxt plugin https://nuxtjs.org/guide/plugins/

export default {
  plugins: ['~/plugins/chat.js']
}

// chat.js
export default ({ store }) => {
   your code that use store here
}

Upvotes: 0

Related Questions