Answer the question
In order to leave comments, you need to log in
Proper location of socket subscriptions in vue?
At the moment, the site is without any assemblies and vuex's, emitters are called in components through methods, and the second vue instance, which is something like a global variable, receives responses.
I want to redo using vue-cli. And here I have a question, where would it be correct to subscribe to events from the outside? In vuex actions, either immediately in mutations, or even out of bounds. All mutations occur only with the signaling from the socket, well, except for authorization, it is through ajax.
What does it look like now
var bus = new Vue({
//служит в роли глобал стейта
data: {
//соответствено все переменные состояния здесь
},
methods: {
//это функция вызывается подписки
subOnEvents: function() {
socket.on('some1',function(e) {
//отсюда уже происходят изменения стейта ака мутации
})
.on('some2',function(e) {})
.on('some3',function(e) {})
.on('some4',function(e) {})
.on('some5',function(e) {});
},
created: function() {
//здесь ajax на авторизация и вызов метода на подписку сокетов
}
}
});
var vm= new Vue({
//используется просто как маунт
el: '#app',
router
});
{
path: 'some',
component: {
template: '<div @click="userWantSomeChanges()">Прям вообще все</div>',
methods: {
//и из их методов уже идут эмиты на сервер
userWantSomeChanges: function() {
socket.emit('serverSideSocket', { bus.user.id };
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question