calog/examples/scripts/libraries/pubsub.lua

25 lines
841 B
Lua

-- Showcase the calog pubsub library within a single Lua context: two handlers
-- subscribe to the "news" topic, a publish delivers to both (count 2), then one
-- handler unsubscribes and a second publish delivers to just one (count 1).
-- A short timer defers the exit so all deliveries land first.
-- Run: bin/calog examples/scripts/libraries/pubsub.lua
local firstId = psSubscribe("news", function(msg)
calogPrint("handler one got:", msg)
end)
local secondId = psSubscribe("news", function(msg)
calogPrint("handler two got:", msg)
end)
local firstCount = psPublish("news", "hello subscribers")
calogPrint("delivered to", firstCount, "handlers")
psUnsubscribe(firstId)
local secondCount = psPublish("news", "only one left")
calogPrint("delivered to", secondCount, "handlers")
timerAfter(150, function()
calogExit(0)
end)