Coder Social home page Coder Social logo

Comments (13)

ma53 avatar ma53 commented on May 17, 2024 29

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.

from vue-socket.io.

gil0mendes avatar gil0mendes commented on May 17, 2024 7

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

from vue-socket.io.

 avatar commented on May 17, 2024 5

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.

from vue-socket.io.

joernroeder avatar joernroeder commented on May 17, 2024 1

@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 })
},

from vue-socket.io.

seanmthompson avatar seanmthompson commented on May 17, 2024

+1

from vue-socket.io.

rodrigorrch avatar rodrigorrch commented on May 17, 2024

+1

from vue-socket.io.

gil0mendes avatar gil0mendes commented on May 17, 2024

+1

from vue-socket.io.

codeofsumit avatar codeofsumit commented on May 17, 2024

+1
is this really not integrated?

from vue-socket.io.

Autsider666 avatar Autsider666 commented on May 17, 2024

+1
Any changes?

from vue-socket.io.

joernroeder avatar joernroeder commented on May 17, 2024

+1

from vue-socket.io.

gil0mendes avatar gil0mendes commented on May 17, 2024

@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.

from vue-socket.io.

Yifei022 avatar Yifei022 commented on May 17, 2024

+1

from vue-socket.io.

joernroeder avatar joernroeder commented on May 17, 2024

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

from vue-socket.io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.