[Vuejs]-How to connect to socket.io from Vue.js component?

4👍

  import VueSocketio from 'vue-socket.io';
  Vue.use(VueSocketio, 'http://localhost:8070/');

This should be done only once before creating Vue instance. Not in component.

Since you’re using Vue-cli webpack, it should be done in main.js before new Vue({})

0👍

import socketio from 'socket.io-client'
import VueSocketIO from 'vue-socket.io'
export const SocketInstance = socketio('http://localhost:8081');
 Vue.use(VueSocketIO, SocketInstance)

0👍

After importing the vue-socket.io package in main.js you may have to instantiate it as a new class

import io from 'socket.io-client'
import VueSocketIO from 'vue-socket.io'
Vue.use(new VueSocketIO({ 
  connection: io('localhost') 
}))

per the docs https://www.npmjs.com/package/vue-socket.io

Leave a comment