0/24
4. Publishing

Lesson 4: Publishing

How does the counter stream update when someone clicks "+1"? The answer is ctx.publish().

ctx.publish('counter', 'set', count);

This sends an event to every client subscribed to the 'counter' topic. The stream receives the event and updates its value.

  • First argument: the topic to publish to (must match the stream's topic)
  • Second argument: the event type ('set' replaces the value)
  • Third argument: the data to send

Try it

The counter only goes up. Add a decrement function that:

  1. Decreases count
  2. Publishes the new value (look at how increment does it)
  3. Returns count

Then import it in the component and add a "-1" button.

WebSocket
0
No messages yet
User A
User B