Thanks, I was wondering whether we can replace callbacks with channels.
The same way it is used in here, instead of calling a function we can also
send the data to a channel associated with the receiver.
like
var subs = make(map[string] []chan string)
func publish(topic string, data string) {
go func() {
if chans, found := subs[topic]; found {
for _, ch := range chans {
ch <- data
}
}
}()
}
func main() {
ch := make(chan string)
ch2 := make(chan string)
subs["simple"] = append(subs["simple"], ch)
subs["simple"] = append(subs["simple"], ch2)
go func() {
time.Sleep(1 * time.Second)
publish("simple", "its so simple")
}()
fmt.Println(<-ch)
fmt.Println(<-ch2)
}
On Monday, March 11, 2019 at 10:42:44 AM UTC+5:30, Randall O'Reilly wrote:
>
> for reference, here is a very basic Qt-style signal / slot style subscribe
> / publish model in Go, just using a map of receivers and their receiver
> callback functions:
> https://github.com/goki/ki/blob/master/ki/signal.go
>
> - Randy
>
> > On Mar 10, 2019, at 11:07 PM, Kasun Vithanage <[email protected]
> <javascript:>> wrote:
> >
> > Yes, when a publisher publish for a topic it should be routed to all
> subscribers to that topic :)
> >
> > On Monday, March 11, 2019 at 10:28:17 AM UTC+5:30, Burak Serdar wrote:
> > On Sun, Mar 10, 2019 at 10:41 PM Kasun Vithanage <[email protected]>
> wrote:
> > >
> > > Hi all,
> > >
> > > I've experience implementing event buses in Java.
> > > In Java, I used a singleton where I can register callbacks to methods
> and fire event from a publisher(maybe from another thread) and propagate it
> to subscribers.
> > >
> > > In Go what would be the best pattern to implement a pub/sub event bus?
> > > Are channels are a better choice than using callbacks in this
> scenario(one to many event propagation)?
> >
> > Are you talking about the event listeners in Java where all listeners
> > are called synchronously? You can't use channels for something like
> > this, and a list of callbacks would be the simplest solution. But you
> > mentioned you might be calling listeners in another thread. Do you
> > need a synchronous call where publish() will make sure all subscribers
> > get the event? If so, maybe you can use a mix of channels and
> > callbacks. You can keep a list of callbacks for listeners in the same
> > goroutine, and a list of channels for listeners in other goroutines,
> > and call/write to elements of both list.
> >
> > > Or callbacks are better for this?
> > >
> > > (I need to propagate events only to subscribers of the topic)
> > >
> > > Regards,
> > > Kasun
> > >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> > > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> > > For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected] <javascript:>.
> > For more options, visit https://groups.google.com/d/optout.
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.