0/24
3. Streams

Lesson 3: Streams

RPCs return a value once. But what about data that changes over time? That's where streams come in.

A stream is a reactive subscription. Every connected client receives the current value and all future updates.

export const myStream = live.stream('topic', () => initialValue, {
  merge: 'set'
});
  • The first argument is the topic - a name that connects publishers and subscribers
  • The second argument is the init function - it returns the starting value
  • merge: 'set' means each update replaces the value entirely

In the component, import the stream and read it with $counter (the dollar sign makes it reactive).

Try it

  1. Add a counter stream to the server file that returns count
  2. Import counter in the component and display $counter

Click "+1" in both previews - both users see the count update in real time.

WebSocket
0
No messages yet
User A
User B