0

I am using vue-socket-io with vuejs2 but facing a problem in the start. I can't seem to make a connection with socket-io. I have followed the exact documentation but every time the call goes it gets an error of 404 not found.

build.js?a270:1 GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=L-T2-Vp 404

This is the error I am getting on the console

@ ./src/main.js 5:0-33 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

I haven't added much line of code in main.js file. Here is the main block that is adding socket

import VueSocketio from 'vue-socket.io';

Vue.use(VueSocketio, 'http://localhost:8080/');
2
  • 1
    You'll get more useful help if you clarify some details. Which documentation are you looking at? What is the main.js file you're referring to? If you're modifying the main.js file in the vue-socket.io library, then that's probably you're problem. vue-socket.io is not a skeleton project that you add your code to, it's a library that you're supposed to use to build your own project. If you modify the library itself, don't be surprised if it ends up breaking. Commented Nov 8, 2017 at 20:01
  • did you find an answer to your problem? Commented May 13, 2018 at 21:15

1 Answer 1

1

you have to create js file and put this code.

var app = require('express')();

var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(4113);

io.on('connection', function (socket) {
    //console.log('connected');
    socket.on('event_name', function (data) {
        //console.log(data);
        io.emit('event_name', data);
    });

});

than you have to install express and sockt-io from terminal when server start than your error will be resolve

run this file : go your path where located file and run node file_name

now, install vue-socket.io vie npm then in main.js file create an instance like:

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

now, you have to put code in your . vue file

sockets: {
    connect() {
      // Fired when the socket connects.
      this.isConnected = true;
      console.log("server connected");
    },

    disconnect() {
      this.isConnected = false;
      console.log("server disconnected");
    },

    // Fired when the server sends something on the "messageChannel" channel.
    messageChannel(data) {
      this.socketMessage = data
    }
    }

now, create to emit server method by this methods...

 pingServer() {
      console.log("click");
      this.$socket.emit('pingServer', 'PING!')
      },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.