Pankaj
Pankaj

Reputation: 10105

Issue while trying to import socket.io in vue.js component in Laravel

I am using Laravel 5.8 with inbuilt vue.js component and socket.io without using redis and laravel-echo-server

npm installation

npm install vue-socket.io

resources/js/app.js file in Laravel

import VueSocketio from 'vue-socket.io';
Vue.use(VueSocketio);

There is no error when compiled using npm run watch command. When I check the output in browser, there is following error.

Cannot call a class as a function

Issue comes in this line: Vue.use(VueSocketio);

Can you please suggest?

Below is Package.json file

"devDependencies": {
    "axios": "^0.18",
    "bootstrap": "^4.1.0",
    "cross-env": "^5.1",
    "jquery": "^3.2",
    "laravel-mix": "^4.0.7",
    "lodash": "^4.17.5",
    "popper.js": "^1.12",
    "resolve-url-loader": "^2.3.1",
    "sass": "^1.15.2",
    "sass-loader": "^7.1.0",
    "vue": "^2.5.17",
    "vue-template-compiler": "^2.6.10",
    "vue-vpaginator": "^1.0.0"
},
"dependencies": {
    "bootstrap-vue": "^2.0.0-rc.24",
    "vee-validate": "^2.2.11",
    "vue-chat-scroll": "^1.3.5",
    "vue-recaptcha": "^1.2.0",
    "vue-socket.io": "^3.0.7",
    "vuejs-dialog": "^1.4.0"
}

Node.js side code. This is a complete different working directory from Laravel

const express = require("express");

class Server {

    constructor() {
        this.app = express();
        this.port = process.env.PORT || 89;
        this.host = process.env.HOST || `192.168.43.173:89`;
    }

    includeRoutes() {
    }

    appExecute() {

        var server = this.app.listen(this.port, () => {
            console.log(`Listening on http://${this.host}`);
        });        
    }
}

const server = new Server();
server.appExecute();

Update code as per suggested by Javas

Vue.use(new VueSocketio({
    debug: true,
    connection: 'http://192.168.43.173:89',
}));

Upvotes: 2

Views: 645

Answers (1)

Andrew Vasylchuk
Andrew Vasylchuk

Reputation: 4779

You should add a new keywoard before VueSocketio.

Vue.use(new VueSocketio({}));

Don't forget to specify a list of options.

Upvotes: 4

Related Questions