Skip to content

Instantly share code, notes, and snippets.

@tomasevich
Created May 22, 2019 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasevich/4bd3559dde3de8ccec1976bd54f1637a to your computer and use it in GitHub Desktop.
Save tomasevich/4bd3559dde3de8ccec1976bd54f1637a to your computer and use it in GitHub Desktop.

Vues

Mini socket plugin for Vue

./src/Vues.vue

/**
 * Vues
 * (c) 2019 Vyacheslav Tomasevich
 *
 * @license MIT
 */

// imports
import io from "socket.io-client";

/**
 * Class Vues
 */
export default class Vues {
  /**
   * Method installing plugin
   * add $socket to global scope
   *
   * @param Vue
   * @static
   */
  static install(Vue) {
    Vue.prototype.$socket = io
  }
}

./src/App.js

<template>
  <div>
    <h1>{{ message }}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0
    };
  },
  props: {
    message: String
  },
  created() {
    let io = this.$socket("http://localhost:3000/");
    io.on("server", count => {
      console.log(io.id, count);
      io.emit("client", this.count++);
    });
  }
};
</script>

./src/main.js

import Vue from "vue";

import App from "./App.vue";
import Vues from "./Vues";

Vue.use(Vues);

new Vue({
  render: h => h(App)
}).$mount("#app");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment