-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing this.$socket from Vuex actions #91
Comments
Hi, @etcogit ✋ Currently there few approaches.
// --- file: socket-instance.js --- //
import io from 'socket.io-client';
export default io('http://socketserver.com:1923'); // <-- use your socket server here and configuration // --- file: main.js --- //
import VueSocketio from 'vue-socket.io-extended';
import $socket from './socket-instance';
Vue.use(VueSocketio, $socket); // --- file: store/showcase/actions.js --- //
import $socket from './socket-instance'; //
export const saveUserLog = ({ commit, rootState }, data) => {
data.user = rootState.globalModule.userConnected._id
commit('addUserLogToTempMemory', data)
$socket.emit('saveUserLog', data) // this doesn't work
}
export function saveUserLog ({ commit, rootState }, data) {
data.user = rootState.globalModule.userConnected._id
commit('addUserLogToTempMemory', data)
this._vm.$socket.emit('saveUserLog', data) // doesn't 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. |
Closing because of inactivity |
Thanks @probil |
I've seen other WebSocket libraries solve this problem by putting the socket instance on the global Vue object something like this: |
@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 export default io(
`${location.protocol}//${location.hostname}:${API_PORT}/${namespace}`,
{
path: '/socket_io_api',
// disable auto-connect to bind connect/disconnect with user xhr login/logout
autoConnect: false,
transportOptions: {
polling: {
extraHeaders: {
Authorization: `JWT ${localStorage.authToken}`,
},
},
},
}); Seems that headers must be fixed and can't update with variable, , and now it will just send sth like |
You're not updating the |
@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. |
You can, just you will need to use an event handler.
…Sent from my iPhone
On 8. Mar 2019, at 02:11, Kai Wu ***@***.***> wrote:
@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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@probil @douira |
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:
How can I access to the $socket instance from within my action ?
I tried
and like suggested in this post
Can you help me?
Thanks
The text was updated successfully, but these errors were encountered: