[Vuejs]-How does one get an active instance of socket.io?

2👍

As Reiner stated in his comment, you can try to save the socket into vuex itself in order to access it from all of your project. For example:

new Vue({
    ...
    created () {
        io.on('connection', socket => {
            this.$store.dispatch('SET_SOCKET', {
                getInstance () {
                    return socket
                }
            })
        })
    }
})

Don’t forget to define the 'SET_SOCKET' actions and mutations.

You will then be able to access it using this.$store.state.socket.getInstance()

I use getInstance() because if I remember correctly the socket mutates itself over time, and VueX does not like that.

Leave a comment