Fork me on GitHub
#core-async
<
2020-06-05
>
souenzzo02:06:02

There is a implementation of "broadcast" in core.async? I mean, when I put into a channel, all listeners will receive the message

(let [pub-port (async/chan)
      pub (async/pub pub-port (constantly ::all))
      sub-port (async/chan)
      sub (async/sub pub ::all sub-port)]
  (async/go
    (prn [1 (async/<! sub)]))
  (async/go
    (prn [2 (async/<! sub)]))
  (async/put! pub-port "hello"))
On this example, just 1 or 2 will be printed. I want both.

hiredman03:06:37

You can have a pub and a mult and tap a channel to both

souenzzo03:06:48

There is no examples of use of mult @hiredman I tryied in this way

(let [pub-port (async/chan)
      mult (async/mult pub-port)
      sub-port (async/chan)]
  (async/tap mult sub-port)
  (async/go
    (prn [1 (async/<! sub-port)]))
  (async/go
    (prn [2 (async/<! sub-port)]))
  (async/put! pub-port "ok"))
With the same result of pub/sub

souenzzo03:06:16

I think I get it. I need to create a new channel and tap it.

souenzzo03:06:51

(letfn [(listen [m]
          (let [c (async/chan)]
            (async/tap m c)
            c))]
  (let [pub-port (async/chan)
        mult (async/mult pub-port)]
    (async/go
      (let [c (listen mult)]
        (prn [1 (async/<! c)])))
    (async/go
      (let [c (listen mult)]
        (prn [2 (async/<! c)])))
    (async/go
      (async/>! pub-port "ok"))))
This listen function make sense? It's implemented somewhere?

raspasov04:06:14

@souenzzo also look at (promise-chan)

raspasov04:06:34

That is, if you need to put one, and only one value on the channel and have all potential consumers receive that value