Skip to content
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

Emit socket event from vuex store #47

Closed
ivoribeiro opened this issue Apr 10, 2017 · 13 comments
Closed

Emit socket event from vuex store #47

ivoribeiro opened this issue Apr 10, 2017 · 13 comments

Comments

@ivoribeiro
Copy link

Hi again, can you give an example how to emit an event from a vuex store action

@seanmthompson
Copy link

+1

2 similar comments
@rodrigorrch
Copy link

+1

@gil0mendes
Copy link

+1

@gil0mendes
Copy link

gil0mendes commented May 6, 2017

Temporarily we can use (new Vue()).$socket.emit(). I know, it's ugly but is better than nothing.

@codeofsumit
Copy link
Contributor

+1
is this really not integrated?

@Autsider666
Copy link

+1
Any changes?

@joernroeder
Copy link

+1

@joernroeder
Copy link

joernroeder commented Jul 8, 2017

@gil0mendes my mutation breaks as soon as i add the hacky line to send the data down the wire and i'm getting a strange value is not a function error.

I'm using vue 2.3.4, vuex 2.3.1 and vue-socket.io 2.1.1

May i'm doing something wrong or is it broken in the meantime? thanks

[types.MY_UPDATE] (state, { index, value }) {
    state.slots[index].value = value

    (new Vue()).$socket.emit('my_update', { index: value })
},

@gil0mendes
Copy link

@joernroeder Hey, the error is really strange. Can you test for me throw the event on an action, instead? Btw, that kind of things is more recommended use on actions.

@Yifei022
Copy link

+1

@ghost
Copy link

ghost commented Nov 18, 2017

While the (new Vue()).$socket.emit workaround is working, I'm currently using this method:

in store.js:

...snip...
state: {
    io: {}
},
mutations: {
    setSocket: (state, socket) => {
      state.io = socket
    }
}
...snip...

in App.vue (or whatever root component you are using)

...snip...
import store from './store'
export default {
  beforeCreate() {
    store.commit('setSocket', this.$socket)
  },
...snip...

That's how the $socket object gets saved to Vuex so that you can access it via context.state.io.

@joernroeder
Copy link

@Dverlik your approroach looks simple and clean – going to use it in the future. thanks for sharing!

@ma53
Copy link

ma53 commented Jan 4, 2018

Originally posted this in #85 but I thought I'd post it here too in case anyone on the chain might find it useful.

Your mutations and actions can reference the root Vue instance via this._vm. So, this is the method I've been using:

someAction(context, payload) {
    this._vm.$socket.emit('someEvent', someData);
}

Avoids instantiating a new Vue instance every time you need to emit an event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants