1

I've been working on allowing a vuejs app talk to a remote, standalone socket.io server. I've managed to get the vuejs app to send messages to the socket.io server (confirmed through console logs on the nodejs instance), and I appear to be getting the responses back, but I can't seem to get it to fire code based on the responses.

I'm using Vue-socket.io, in the most basic form and i've added localhost and null to origins to hopefully rule out that issue.

  • I'm running socket.io server on localhost:3000
  • I'm running vuejs app on localhost:8888

Why aren't the listeners firing in the following code? I also don't get any of the console logs for sockets.connect and sockets.customMessage in app.vue.

socket.io (nodejs) server:

var http = require('http').createServer({
    origins: ['localhost','null',null]
});
var io = require('socket.io')(http);

io.on('connection', (socket) => {
    console.log('a user connected');
    socket.broadcast.emit('hi, welcome to live chat');
    socket.on('disconnect', () => {
        console.log('user disconnected');
    });
    socket.on('chatMessage', (msg) => {
        console.log('chatMessage: ' + msg);
        io.emit('chatMessage', msg);
    });
})



http.listen(3000, () => {
    console.log('listening on port 3000');
});

app.js (entry point for vuejs app):

import Vue from 'vue'
//import store from './store'
import App from './App.vue'
import VueSocketIO from 'vue-socket.io'
import SocketIO from "socket.io-client"

Vue.use(new VueSocketIO({
    debug: true,
    connection: 'http://localhost:3000'
}))

new Vue({
    render: h => h(App)
}).$mount('#app')

App.vue:

<template>
    <div>
        hello chat app
        <input type="text" v-model="message"/>
        <button @click="clickButton()">Send Msg</button>
    </div>
</template>

<script>
    export default {
        name: "Home",
        sockets: {
            connect: function () {
                console.log('socket connected')
            },
            chatMessage: function(data) {
                console.log('this method was fired by the socket server. eg: io.emit("chatMessage", data)')

            }
        },
        methods: {
            clickButton: function () {
                console.log('button clicked');
                console.log(this.message);
                // $socket is socket.io-client instance
                this.$socket.emit('chatMessage', this.message)
            }
        },
        data: () => {
            return {
                message: '',
            };
        },
        computed: {

        },
        mounted() {
            this.$socket.on('chatMessage',data => {
                console.log('listen fired')
                console.log(data);

            });
        }
    }
</script>
David
  • 16,246
  • 34
  • 103
  • 162
  • Is it an option to you work with a pure socket.io solution ? I mean without the lib vue-socket.io? Using only socket.io (server side) and socket.io-client? – Danizavtz May 20 '20 at 10:34
  • @Danizavtz more than happy to go that route too. It's going to be a live support chat app, so I imagine you can consider the flexibility i'm likely to need in the future. – David May 20 '20 at 10:35
  • I suspect my problem here is that i'm trying to listen to the socket at a component (app.vue) level, instead of the full app level. – David May 20 '20 at 12:48

2 Answers2

1

I created an VueApp, then copied, pasted your code to use. It's working well.

So what I changed in your code is just: comment out the unused code: enter image description here

Hope this helps!

Thang Duc
  • 316
  • 1
  • 5
  • @Thanng Duc yes, but the console logs on the vueapp, which are suppose to listen for responses aren't firing. – David May 20 '20 at 12:38
  • @David could you try vue-socket.io-extended I found it in https://stackoverflow.com/questions/55030370/vuejs-with-vue-socket-io-not-displaying-anything. I also tried some ways with the vue-socket-io, but doesn't work. Then I changed it to https://www.npmjs.com/package/vue-socket.io-extended and it's working well now. (no need to listen events in mounted() ) – Thang Duc May 20 '20 at 15:23
  • I've decided to give vue-socket.io-extended a go and it seems to work alot better and i'm now getting my responses. – David May 21 '20 at 07:25
0

Can you try to put this configuration in your app.js

Just add a option for connection, and use it when instantiating the VueSocketIO instance.

const options = { path: '/socket.io/' }; //<--

Vue.use(new VueSocketIO({
    debug: true,
    connection: 'http://localhost:3000',
    options //<--
  })
);

And try again? If it does not work, I can post mine solution.

Danizavtz
  • 3,166
  • 4
  • 24
  • 25
  • Still not working. I'm getting all the same responses as Thang shows in, but the console logs simply aren't firing which are suppose to when we get a response on the veuapp. I'm starting to wonder if i'm not sending the responses in the correct format. – David May 20 '20 at 12:40