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:
- Build a backend server. Here’s a Node.js guide
- 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
- 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.
- 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/
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:
- change todos:” to todos:[]
- 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