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>
3
  • 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? Commented May 20, 2020 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. Commented May 20, 2020 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. Commented May 20, 2020 at 12:48

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

@Thanng Duc yes, but the console logs on the vueapp, which are suppose to listen for responses aren't firing.
@David could you try vue-socket.io-extended I found it in stackoverflow.com/questions/55030370/…. I also tried some ways with the vue-socket-io, but doesn't work. Then I changed it to npmjs.com/package/vue-socket.io-extended and it's working well now. (no need to listen events in mounted() )
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.
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.

1 Comment

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.

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.