This repository was archived by the owner on Jun 18, 2025. It is now read-only.
This repository was archived by the owner on Jun 18, 2025. It is now read-only.
Accessing this.$socket from Vuex actions #91
Closed
Description
Hi,
In my project's folder structure I splitted my Vuex actions (and also mutations, getters and state) into separate files, like suggested here.
Here is an example of an action.js file:
export const saveUserLog = ({ commit, rootState }, data) => {
data.user = rootState.globalModule.userConnected._id
commit('addUserLogToTempMemory', data)
this.$socket.emit('saveUserLog', data) // this doesn't work
}
How can I access to the $socket instance from within my action ?
I tried
export const saveUserLog = ({ commit, rootState, Vue }, data) => { // adding Vue here
data.user = rootState.globalModule.userConnected._id
commit('addUserLogToTempMemory', data)
Vue.$socket.emit('saveUserLog', data) // to access from here
}
and like suggested in this post
export const saveUserLog = ({ commit, rootState }, data) => {
data.user = rootState.globalModule.userConnected._id
commit('addUserLogToTempMemory', data)
this._vm.$socket.emit('saveUserLog', data) // doesn't work
}
Can you help me?
Thanks
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
probil commentedon Jun 14, 2018
Hi, @etcogit ✋
I'm glad to see an issue from you again. 👍
Currently there few approaches.
this
in arrow function is not what we actually need. Vuex can't bind vuex module asthis
if arrow function is used. E.g. this should work:Those approaches i not perfect. Currently I'm trying to figure out how to make socket usage in actions simplier. If someone has any idea I will be glad to hear and discuss. 🤝
@etcogit let me know if that helped you.
probil commentedon Jun 20, 2018
Closing because of inactivity
xino1010 commentedon Dec 5, 2018
Thanks @probil
douira commentedon Mar 5, 2019
I've seen other WebSocket libraries solve this problem by putting the socket instance on the global Vue object something like this:
Vue.$socket
.Is this still even a problem or is has this already been solved with something like
Vue.$socket
?quietcoolwu commentedon Mar 7, 2019
@probil I wonder how can we set a header for auth, on the connection of socket instance (and not in the static config of instance), here's my instance config
Seems that headers must be fixed and can't update with variable, , and now it will just send sth like
JWT undefined
in handshaking becauselocalStorage.authToken === undefined
when we define the socket.io instance, andlocalStorage.authToken
will only be updated to a valid token after user login auth finished.douira commentedon Mar 7, 2019
You're not updating the
Authorization
field here at all though. Once the file has been run, the field will not change because the code is only run once on import.quietcoolwu commentedon Mar 8, 2019
@douira does that mean I cannot check the socket.io connection for auth on the serverside for all? My target is to check each client connection handshake from the headers.
douira commentedon Mar 8, 2019
quietcoolwu commentedon Mar 24, 2019
@probil @douira
could you gimme a specific example on how to use an event handler for token auth?
If I import
$socket
from an outer instance which is kind of pre-configuired, how can I get the header via localStorage or vuex getters, which may vary and add the header to this instance? and which event should I bind the auth procedure?