iii-stream is for real-time data transmission: pushing data to a client the moment it changes,
like a live feed of clicks for a dashboard. A stream is bidirectional (subscribers can send messages
back as well as receive them), but here you only need to broadcast clicks outward. You’ll move the
live-broadcast concern into its own click-streamer worker so the link worker stays focused on
links.
Add the workers
iii-stream is how we will send clicks to clients in Chapter 7. We’ll make a new click-streamer
worker to manage the streaming, so scaffold it the same way you scaffolded link in Chapter 1, and
analytics in Chapter 4:
Broadcast clicks in real time
We’ll continue to keeplink decoupled by having it announce that a click happened, and
click-streamer reacts by pushing it onto the live feed. A live counter can tolerate the rare
dropped event, so a regular iii-pubsub event is the right tool here.
Add a link.clicked event to the link worker
First, publish a link.clicked event from link::record_click:
link/src/index.ts
In the new code above we didn’t use
await and set the action to TriggerAction.Void(). This
causes the function to return immediately before it completes. This is a simple performance
enhancement with things like pubsub where we don’t need guaranteed execution.Setup the click-streamer worker
Now write the click-streamer worker. It subscribes to link.clicked and broadcasts each click to
a clicks stream with stream::set. A stream::set both stores the item and pushes it to every
WebSocket subscribed to that stream and group. Replace the generated click-streamer/src/index.ts:
click-streamer/src/index.ts
clicks/all and counts those broadcasts live.
See it work
With the engine running, follow a link a few times and read the liveclicks stream:
click-streamer worker broadcasts it.
Conclusion
Linkly now streams every click to subscribers in real time through a dedicatedclick-streamer
worker. Next, in Ch. 6: Move bulk data with channels, you bulk-load
links from a CSV in a single streamed upload.