I'm trying to use websockets (socket-io) in my Vue project and it seems a plugin would be the best way to do so. My problem is I need to establish a connection after the user logs in because the user will be specifying the ip address of backend server that they're going to connect to. All the documentation I've seen requires plugins to be added to the store as part of initialization which doesn't help me, I need to add or update a plugin after login.
If there's another pattern for using websockets, I'm all ears. Thank you!
CodePudding user response:
as the documentation says
A Vuex plugin is simply a function that receives the store as the only argument
And it is indeed as simple as that. When we look at vuex constructor we see that all it does is calls every method in the plugins array and provides this
as the first and only argument.
So you can create your store like normal and then when you're ready you can just call your plugin function and pass the previously created store instance to it.