0

I am using vue2 syntax and vuex , versions : vue/cli 4.5.13 and vue@2.6.14 and vuex 3.6.2

I have a simple to do project , for adding todos in a list, based on the 2019 vue tutorial by traversy.

I have a simple form ij my component to add a to do <form @submit.prevent="onSubmit" >

and in my vuex store I have

const state = {
    todos:''

};

const getters = {
    allTodos: (state) => {return state.todos}
};

const actions = { 
    async addTodo({commit}, title){
        const res = await axios.post('https://jsonplaceholder.typicode.com/todos', {
            title, 
            completed:false
        }); 
        commit('newTodo', res.data);
    }    
};

const mutations = { 
    newTodo:(state, todo)=>( 
        state.todos.unshift(todo)
    )
};

Is there a way to update all clients that view the todos, without clients have to refresh nothing, as soon as a new todo is added in the state, using only vuex/vue?

Thank you

codebot
  • 517
  • 8
  • 29
  • 55

2 Answers2

3

Is there a way to update all clients that view the todos, without clients have to refresh nothing, as soon as a new todo is added in the state, using only vuex/vue?

No, it is not possible.

There is no link between all your clients. All your Vue/VueX code lives in a single client. Here's what you need to do to get where you want to go, and its a long way from here:

  1. Build a backend server. Here's a Node.js guide
  2. Build an APi in your server. Your clients will make requests to this server to get all todos, and post new todos to the server. Here's an express.js guide
  3. You need a database to store your todos in the server. You can use something like MongoDB or an ORM like Sequelize for node.js.
  4. Now you can either write a code to periodically request the server for todos in the background and update it in your vue components, or you can use a pub/sub library like pusher. Pusher uses WebSockets under the hood for maintaining a persistent bidirectional connection. If you want to, you can implement this on your own, you can read about it here, thanks to @Aurora for the link to the tutorial.

Here's a consolidated guide for doing all this: https://pusher.com/tutorials/realtime-app-vuejs/

Mythos
  • 1,378
  • 1
  • 8
  • 21
  • 1
    Mythos is right. Also you can check websocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications – Aurora Sep 13 '21 at 21:26
  • @Aurora Thanks for the link. Interestingly, Pusher works on WebSockets under the hood. I'll add your link to the answer. – Mythos Sep 15 '21 at 10:23
1

Is there a way to update all clients that view the todos, without clients have to refresh nothing, as soon as a new todo is added in the state, using only vuex/vue?

There's a couple of errors in your code:

  1. change todos:'' to todos:[]
  2. change state.todos.unshift(todo) to state.todos.push(todo)

This way, every time that you call addTodo action, all components connected to allTodos getter will show the latest todos

NOTE:

Vuex/Vue are reactive. So in every page that you see using that showcomponent will show you the last update. If you want to show in every USER CONNECTED, of course you don't need http request, you need WEBSOCKETS

Alex Hunter
  • 212
  • 9
  • 30
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/29829038) – Erik Sep 15 '21 at 07:34
  • I am answering the question, not criticizing – Alex Hunter Sep 15 '21 at 11:50
  • You are right, my bad. It would be clearer if the proposed solution was not described as errors in the code. – Erik Sep 15 '21 at 12:37
  • In context of the question `update all clients that view the todos, without clients have to refresh nothing`, your answer `every page that you see using that showcomponent will show you the last update` is somewhat misleading. Even if there's only a single user on a single machine, the VueX state will still be limited to a single browser tab (not even that if OP is using href links). What I mean is its not about `every USER CONNECTED`, its about VueX state persistency, something you could workaround with some [setup](https://stackoverflow.com/questions/43027499/vuex-state-on-page-refresh?rq=1) – Mythos Sep 15 '21 at 17:50
  • if you want to persist state data, use vuex-persistedstate @Mythos – Alex Hunter Sep 17 '21 at 14:57
  • Yes I linked it in my comment – Mythos Sep 17 '21 at 15:16