How to Use SSE for Real-Time Updates Across Multiple Stores in Nuxt 3 with Pinia
Hey everyone!
I'm working on a Nuxt 3 project using Vue, Vuetify, and Pinia, and I’m implementing real-time updates with Server-Sent Events (SSE) as described in the Nitro WebSocket guide. I’ve successfully set up an SSE endpoint at /sse, and I can receive messages without any issues.
However, I’m stuck on two points:
How can I send SSE messages from other API routes (like /api/updateNote.ts) to notify changes globally?
How can I subscribe to these SSE updates in a way that works across my entire app, rather than manually subscribing in each store or component?
For example, I want my Pinia store for "notes" to automatically update when an SSE message arrives, regardless of the current page. Any advice or best practices would be greatly appreciated!
Thanks in advance!
6 Replies
Hi @Toffe. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Using SSE for Real-Time Updates with Pinia in Nuxt 3
Your question about implementing Server-Sent Events (SSE) for real-time updates across multiple Pinia stores in Nuxt 3 is a common architecture challenge. Let me help you with this based on the available information.
Setting Up SSE in Nuxt 3
Nuxt 3 and Nitro have built-in support for SSE. According to the documentation, you can create an event stream using the
createEventStream
utility:
Sending SSE Messages from Other API Routes
To send messages from other API routes (like your /api/updateNote.ts
), you need a way to access active SSE connections. This requires implementing a simple pub/sub system in your Nitro server.
Here's how you can approach this:
Then in your SSE endpoint:
And in your update API:
Subscribing to SSE Updates Across Your App
For the client side, you can create a composable that manages the SSE connection and dispatches updates to your Pinia stores. This approach centralizes the SSE handling logic:
Then in your Pinia stores, implement the handler method:
Using the SSE Connection in Your App
Finally, you can use this composable in your app.vue or a layout file to ensure it's always active:
Alternative Approach Using POST Requests for SSE
If you need to send data to the SSE endpoint (like for authentication), you can use the POST method as described in the Nuxt documentation:
This approach is documented in the Nuxt data fetching guide for consuming SSE via POST requests Nuxt Data Fetching Guide.
By implementing this architecture, you'll have a centralized system for real-time updates that works across your entire Nuxt application, with each Pinia store handling its own update logic.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@260145305754927104> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai please rephrase in typescript only.
Let me check the knowledge base for you again. One moment! :books:
----------------------
I'll provide a TypeScript implementation of the SSE solution for real-time updates across multiple Pinia stores in Nuxt 3.
Server-Side Implementation
Client-Side Implementation
This TypeScript implementation provides type safety throughout your SSE system, from server-side connections to client-side store updates. The approach using POST requests for SSE is based on the pattern described in the Nuxt Data Fetching Guide.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@260145305754927104> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
For humans: Is this a good method to "lock" documents e.t.c. in real time so other people do not edit them? 🙂 I want to load all data into the browser, then once something is updated / edited they do pull