0/12
2. Understanding the Counter

Lesson 2: Understanding the Counter

You've seen the counter work. Now make it your own.

The key concepts:

  • live(fn) wraps a server function as an RPC. The first argument is always ctx.
  • ctx.publish(topic, event, data) sends an event to all clients subscribed to that topic.
  • live.stream(topic, init, options) creates a reactive subscription. The topic string must match what your RPCs publish to.
  • merge: 'set' means each event replaces the store value entirely.

Try it

Rename the topic from "counter" to "score" in both the RPC and the stream. Rename the stream export to score. Add a decrement function. Update the component to use the new names.

The topic string in ctx.publish() must match the topic in live.stream() - if they don't match, subscribers never receive updates.

WebSocket
0
No messages yet
User A
User B