A
A
Alexey2018-03-15 23:48:53
webpack
Alexey, 2018-03-15 23:48:53

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
});

all components are located directly in the router as html code
{
  path: 'some',
  component: {
    template: '<div @click="userWantSomeChanges()">Прям вообще все</div>',
    methods: {
      //и из их методов уже идут эмиты на сервер
      userWantSomeChanges: function() {
        socket.emit('serverSideSocket', { bus.user.id };
      }
    }
  }
}

Will switching to cli-vue + vuex + assembly give any advantages, provided that 1 person always works on the project. Of the obvious to me, this is only a smaller size of the main script and that's it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question