Create a write-only
TChan. More precisely,
readTChan
will
retry even after items have been written to the channel.
The only way to read a broadcast channel is to duplicate it with
dupTChan.
Consider a server that broadcasts messages to clients:
serve :: TChan Message -> Client -> IO loop
serve broadcastChan client = do
myChan <- dupTChan broadcastChan
forever $ do
message <- readTChan myChan
send client message
The problem with using
newTChan to create the broadcast channel
is that if it is only written to and never read, items will pile up in
memory. By using
newBroadcastTChan to create the broadcast
channel, items can be garbage collected after clients have seen them.