I'm trying to use Spring websockets (STOMP) with Vue but can't figure out how to do it or if it's even possible. My websockets work with plain JS but when i try with Vue i get stuck. Here is my vue code:
var app = new Vue({
el: '#app',
data: {
stompClient: null,
gold: 0
},
methods: {
sendEvent: function () {
this.stompClient.send(/app/hello, {}, JSON.stringify({'name': $(#name).val()}));
}
},
created: function () {
this.stompClient = Stomp.over(new SockJS('/gs-guide-websocket'));
this.stompClient.connect()
this.stompClient.subscribe('/topic/greetings', function (greeting) {
console.log(JSON.parse(greeting.body).content);
});
},
})
My connect and send function is working and i can see the message in the backend but the problem is the subscribe function. It needs a callback function but this never fire. I have also tried to create a method in vue and call that
this.stompClient.subscribe('/topic/greetings', vueFunc())
but that does not work either. I have found some library at https://github.com/FlySkyBear/vue-stomp but I can't figure out how to use that and it looks really messy. I rather use plain JS then.
Anyone got an solution? Thanks