All situations
Three components, one live connection.
The first subscriber opens it. The last one closes it.
WebSocketonmessageclose()
export const $prices = atom<Prices>({})
onMount($prices, () => {
const ws = new WebSocket(PRICES_URL)
ws.onmessage = (event) => $prices.set(JSON.parse(event.data) as Prices)
return () => ws.close()
})first subscriberOpens
more subscribersSame connection
last one leavesCloses
Already have one shared connection? You don't need this.
npm install nanostoresTwo islands need the same data →One state for several frameworks →