[Vuejs]-How to use Nuxt.js with socket.io/socket.io.-client?

0👍

Hi I use it it my app as a plugin:

Define it in socket.js file like this:

import SocketIO from 'socket.io-client';
import VueSocketIoExtended from 'vue-socket.io-extended';
import Vue from 'vue';

export default ({ store, env }) => {
    const socket = SocketIO(env.socketUrl, {
        transports: ['websocket'],
        reconnection: true,
        reconnectionAttempts: Infinity,
        autoConnect: true,
    });

    Vue.use(VueSocketIoExtended, socket, { store });
};

then you can include it in nuxt.config.js as plugin:

  plugins: [
        { src: '~/socket.js', mode: 'client' },
    ],

More info about socket library here:
https://www.npmjs.com/package/vue-socket.io-extended

Leave a comment