because shithub doesnt have ipv6 support
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
Go to file
Metin Seylan 2f17ac9a17 Merge pull request #27 from pieterjandesmedt/master
fix: add listeners to custom socket functions on the `created` hook
8 years ago
dist fix: add listeners to custom socket functions on the `created` hook instead of `beforeCreate` hook in the mixin lifecycle 8 years ago
src fix: add listeners to custom socket functions on the `created` hook instead of `beforeCreate` hook in the mixin lifecycle 8 years ago
.gitignore v2 8 years ago
README.md Update README.md 8 years ago
package.json 2.1.0 8 years ago
webpack.config.js 2.1.0 8 years ago

README.md

Vue-Socket.io

NPM version VueJS v2 compatible Downloads Dependency Badge License

socket.io implemantation for Vuejs 2 and Vuex

Install

npm install vue-socket.io --save

Usage

Configuration

Automatic socket connection from an URL string

Vue.use(VueSocketio, 'http://socketserver.com:1923');

Bind custom socket.io-client instance

Vue.use(VueSocketio, socketio('http://socketserver.com:1923'));

Enable Vuex integration

import store from './yourstore'
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);

On Vuejs instance usage

var vm = new Vue({
  sockets:{
    connect: function(){
      console.log('socket connected')
    },
    customEmit: function(val){
      console.log('this method was fired by the socket server. eg: io.emit("customEmit", data)')
    }
  },
  methods: {
    clickButton: function(val){
        // $socket is socket.io-client instance
        this.$socket.emit('emit_method', val);
    }
  }
})

Dynamic socket event listeners

Create a new listener

this.$options.sockets.event_name = (data) => {
    console.log(data)
}

Remove existing listener

delete this.$options.sockets.event_name;

Vuex Store integration

Example store, socket mutations always have "SOCKET_" prefix

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        connect: false,
        message: null
    },
    mutations:{
        SOCKET_CONNECT: (state,  status ) => {
            state.connect = true;
        },
        SOCKET_USER_MESSAGE: (state,  message) => {
            state.message = message;
        }
    },
    actions: {
        otherAction: ({ commit, dispatch, state }, type) => {
            return true;
        }
    }
})

Example

Realtime Car Tracker System

Simple Chat App