Cnator: channel-based subscriptions in Go

Channels is one of those things that makes Go awesome.

I use channels quite a lot in my next open projects. One of those scenario is to model an event-driven approach.

While developing these projects, I noticed that the setup is more or less the same…


This content originally appeared on DEV Community and was authored by Altiano Gerung

Channels is one of those things that makes Go awesome.

I use channels quite a lot in my next open projects. One of those scenario is to model an event-driven approach.

While developing these projects, I noticed that the setup is more or less the same, so I decided to make a Go module for that.

It's called Cnator (pronounce "c-nator")

go get gitlab.com/altiano/cnator

It has 3 methods :

  • New()
  • Subscribe(channel, subscriber)
  • Serve()

that can be use it like this

    cnator := cnator.New()
    channels := createChannels()

    // provide channel reference to each publisher
    producerA := NewProducerA(channels.chanA)
    producerB := NewProducerB(channels.chanB)
    producerC := NewProducerC(channels.chanC)

    // using cnator to subscribe to those channel events
    subscriberA := subscriberA{}
    subscriberB := subscriberB{}
    subscriberC := subscriberC{}
    cnator.Subscribe(channels.chanA, subscriberA.receiveChanA)
    cnator.Subscribe(channels.chanB, subscriberB.receiveChanB)
    cnator.Subscribe(channels.chanC, subscriberC.receiveChanC)

     // start watching for events
    cnator.Serve()

createChannel() just initialized the channels, but you should provide your own model

func createChannels() channels {
    return channels{
        chanA: make(chan struct{}),
        chanB: make(chan string),
        chanC: make(chan Person),
    }
}

the subscribers are just callback functions with a matching data type with the channels

func (s *subscriberA) receiveChanA() {
    fmt.Println("Subscriber A receiving")
}

func (s *subscriberB) receiveChanB(data string) {
    fmt.Println("Subscriber B receiving", data)
}

func (s *subscriberC) receiveChanC(data Person) {
    fmt.Println("Subscriber C receiving", data)
}

except for chanA that receive empty struct{},
you can ignore the parameter like receiveChanA() does.

The job of cnator.Serve() is to spawn a go routine for each subscription made by cnator.Subscribe(..).

It provides some runtime validation like

  • whether the subscriber function doesn't satisfy the channel data type or
  • whether the channel has not been initialized (i.e. forget to make(chan ..)) etc

Full code & examples can be found at this repository : Cnator

Also posted on : https://blog.altiano.dev/cnator-channel-based-subscription-in-go


This content originally appeared on DEV Community and was authored by Altiano Gerung


Print Share Comment Cite Upload Translate Updates
APA

Altiano Gerung | Sciencx (2021-05-26T16:17:03+00:00) Cnator: channel-based subscriptions in Go. Retrieved from https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/

MLA
" » Cnator: channel-based subscriptions in Go." Altiano Gerung | Sciencx - Wednesday May 26, 2021, https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/
HARVARD
Altiano Gerung | Sciencx Wednesday May 26, 2021 » Cnator: channel-based subscriptions in Go., viewed ,<https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/>
VANCOUVER
Altiano Gerung | Sciencx - » Cnator: channel-based subscriptions in Go. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/
CHICAGO
" » Cnator: channel-based subscriptions in Go." Altiano Gerung | Sciencx - Accessed . https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/
IEEE
" » Cnator: channel-based subscriptions in Go." Altiano Gerung | Sciencx [Online]. Available: https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/. [Accessed: ]
rf:citation
» Cnator: channel-based subscriptions in Go | Altiano Gerung | Sciencx | https://www.scien.cx/2021/05/26/cnator-channel-based-subscriptions-in-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.