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 create it the same way you created link in Chapter 1 and
analytics in Chapter 4. iii-stream must start with the engine, so declare it in the Compose
engine configuration:
worker-compose.yaml
click-streamer/iii.worker.yaml
click-streamer/package.json
Broadcast clicks in real time
Havelink announce each click with a pubsub event. Then, have click-streamer push the event
onto the live feed.
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. Create click-streamer/src/index.ts:
click-streamer/src/index.ts
clicks/all and counts those broadcasts live.
See it work
With the engine running, create and follow a link a few times:clicks 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.