Using nuxt-socket-io with Pinia - vue.js

I want to write my application like a chat on Nuxt.Js 3 and use Pinia as a store. I found the nuxt-socket-io module - the easiest way for me to implement my chat. I read full docs, but only vuex usage and some kind of iox were mentioned there. Iox saves all the data in the state, but I need to call mutation/action from store when emitting an event on the server side. Question: how can I link nuxt-socket-io events with Pinia actions?
I tried to declare Pinia actions in vuex configuration in io.config. Then i tried to pass global $ioState value to Pinia state.

Related

Why use Vuex in SSR?

My question is, what's the point of setting up Vuex for the server when the state will be overwritten when the client side hydration takes place?
I have some data (Helm env variables) that I want to store in the vuex store for later use.
These variables is only available to me on the server, so I started trying to add them to the store in my createApp script when running on the server.
The store state however is reset when the client side hydration kicks in, so no env variables left.
Google told me I should use like window.INITIAL_DATA to set the state again on the client:
store.replaceState(window.INITIAL_DATA)
But if have to use the window object to pass store data to the client, what's the point of using Vuex on the server at all?
Isn't it better to skip Vuex overhead on the server and just use Vuex on the client and populate it with INITIAL_DATA?
I'm probably missing something..
https://ssr.vuejs.org/guide/data.html#data-store
During SSR, we are essentially rendering a "snapshot" of our app. The asynchronous data from our components needs to be available before we mount the client side app - otherwise the client app would render using different state and the hydration would fail.
To address this, the fetched data needs to live outside the view components, in a dedicated data store, or a "state container". On the server, we can pre-fetch and fill data into the store while rendering. In addition, we will serialize and inline the state in the HTML after the app has finished rendering. The client-side store can directly pick up the inlined state before we mount the app.
Also to mention:
The data you access while SSR in any Component needs to come from somewhere if you want to share information across Components, this is what Vuex is there for.

Vuex: Propogate response to sibling components

I have turned several axios requests made by application from 3 sibling components into one. So this query now brings back all the information in one simple request. Trouble is I'm not sure architecturally, where it makes sense to perform the request, store the data and propogate the response to the three sibling components.
Should I store the response in an application-Level shared state ? Also then how do I propogate the response, to these 3 sibling comnponents using Vuex?
if it is critical data then you might want to request it in Vuex nuxtServerInit action and save it in vuex state using commit. Read more
That way your app will receive the info once when it renders on server & can be used from any page or component using vuex getters.

Vuex module reset state for SSR

I am using vuejs with vuex as state management.While doing SSR,I am creating fresh instance of app with fresh instance of router and store,
to avoid Stateful Singletons in server.More in this can be found here:https://ssr.vuejs.org/guide/structure.html#avoid-stateful-singletons.
The problem is my modules state in vuex store don't get reset.I didn't find a suitable way to create a fresh instance of a module.
I don't want to use new vmcontext in node server,also don't want to reset state of same module everytime for SSR.

Where does Vuex store reside in Nuxt

Just trying to get my head around Vuex / Nuxt , also quite new to nodejs.
Given that nuxt is a server side rendering app , Where does Vuex "reside" . In standard Vue , Vuex is a client side store. How does it work in Nuxt? Is it still a client side store but just rendered on the server first?
If it stays on the server , then I am just wondering how it handles per user data - or is it somehow shared. thank you.
SSR and rehydration:
Both client and server hold the state. When the client makes the first request to load the initial page the state is usually passed through the html document inside a script, something like:
<script type="text/javascript" defer>window.__NUXT__={"data":[{"name":"server"}],"error":null,"serverRendered":true}</script>
Then when the application js load it has to pickup the state, this processed is known as rehydration. From this point the client will maintain the server state in sync.
Nuxt specifics:
Interesting parts of Nuxt documentation:
https://nuxtjs.org/guide/async-data
https://nuxtjs.org/guide/vuex-store
Nuxt Vuex Demo:
https://nuxtjs.org/examples/vuex-store
Vue SSR:
In addition take a look into vue ssr documentation, it's very detailed and it does a better job explaining how everything fits in:
https://ssr.vuejs.org/en/hydration.html
https://ssr.vuejs.org/en/data.html
we will serialize and inline the state in the HTML. The client-side store can directly pick up the inlined state before we mount the app.

Vue js 2 Persist a component's data *privately*

I currently have a Vue.js 2 SPA using Vuex for inter-component communication and Vuex-persisted state to persist my shared state.
However, I want to persist a component's local private data.
I tried the package vue-persist and it persists the component data fine, however, the variables are not namespaced and therefore shared between all component instances. I want to persist each component's data separately.
Does anyone know how I can achieve these 2 things in my Vue.js 2.x app?
1) Persistence of my local data (so it survives a browser refresh).
2) The persisted data is not shared with other components (perhaps name-spaced for each component instance?).
Thanks!
I'm not familiar with vue-persist, but it looks like you can pass a store name as the second argument to $persist. Choose a unique store name for the component instance (this will vary depending on the component).
e.g.
created() {
this.$persist(['foo'], 'my-comp:' + this.id);
}