vue-socket.io

socket.io implementation for Vue.js and Vuex
474,562 3,890
Install
npm install vue-socket.io
Latest Version:3.0.10
Older releases:2
License:MIT
Last Updated:Feb 9, 2023
Links: GitHub  ·  npm
Maintainer: metinseylan
Demo
  • Chat Application
  • Car Tracking Application
🚀 Installation
npm install vue-socket.io --save
Using Connection String
import Vue from 'vue'
import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'

Vue.use(new VueSocketIO({
    debug: true,
    connection: 'http://metinseylan.com:1992',
    vuex: {
        store,
        actionPrefix: 'SOCKET_',
        mutationPrefix: 'SOCKET_'
    },
    options: { path: "/my-app/" } //Optional options
}))

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
Using socket.io-client Instance
import Vue from 'vue'
import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'

const options = { path: '/my-app/' }; //Options object to pass into SocketIO

Vue.use(new VueSocketIO({
    debug: true,
    connection: SocketIO('http://metinseylan.com:1992', options), //options object is Optional
    vuex: {
      store,
      actionPrefix: "SOCKET_",
      mutationPrefix: "SOCKET_"
    }
  })
);

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
Parameters Type's Default Required Description
debug Boolean false Optional Enable logging for debug
connection String/Socket.io-client null Required Websocket server url or socket.io-client instance
vuex.store Vuex null Optional Vuex store instance
vuex.actionPrefix String null Optional Prefix for emitting server side vuex actions
vuex.mutationPrefix String null Optional Prefix for emitting server side vuex mutations
🌈 Component Level Usage
new Vue({
    sockets: {
        connect: function () {
            console.log('socket connected')
        },
        customEmit: function (data) {
            console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
        }
    },
    methods: {
        clickButton: function (data) {
            // $socket is socket.io-client instance
            this.$socket.emit('emit_method', data)
        }
    }
})
Dynamic Listeners
this.sockets.subscribe('EVENT_NAME', (data) => {
    this.msg = data.message;
});

this.sockets.unsubscribe('EVENT_NAME');
🏆 Vuex Integration
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {},
    mutations: {
        "<MUTATION_PREFIX><EVENT_NAME>"() {
            // do something
        }
    },
    actions: {
        "<ACTION_PREFIX><EVENT_NAME>"() {
            // do something
        }
    }
})

Stargazers over time

Stargazers over time

Related Packages

vue-3-socket.io

socket.io implementation for Vue.js and Vuex

49,565 42
vue-socket.io-extended

Socket.io bindings for Vue.js and Vuex (inspired by Vue-Socket.io)

207,114 625
vue-native-websocket-vue3

支持vue3和vuex、pinia的websocket插件

162,849 221
@hlf01/vue3-socket.io

Socket.io implementation for Vue.js 3.0

13,353 2

Version History

Version Released License
3.0.10 MIT
3.0.9 MIT
3.0.8 MIT
3.0.7 MIT
3.0.6 MIT
Pre-releases (2)
Version Released License
2.1.1-b MIT
2.1.1-a MIT