Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/xmedia-systems/caster

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/xmedia-systems/caster

  • v0.0.0-20220818101738-e8298fbef3d9
  • Source
  • Go
  • Socket score

Version published
Created
Source

caster

GoDoc

caster is a dead simple and performant message broadcaster for Go with context support. It uses the publisher and subscriber pattern (pubsub) to broadcast messages from a single or multiple source channels to multiple subscriber channels. Subscribers can dynamically join and leave.

Usage

Broadcast a Go channel

Suppose the Go channel is:

var srcCh <-chan interface{}

We can broadcast the messages coming out of it to multiple subscribers:

c := caster.New(nil)

go func() {
    // subscriber #1
    ch, _ := c.Sub(nil, 1)

    for m := range ch {
        // do anything to the broadcasted message
    }
}()

go func() {
    // subscriber #2
    ch, _ := c.Sub(nil, 1)

    for m := range ch {
        // do anything to the broadcasted message
    }
}()

go func() {
    // publisher
    for m := range srcCh {
        c.Pub(m)
    }

    c.Close()
}()

Subscribers can join and leave at any time:

// join
ch1, _ := c.Sub(nil, 1)

// leave
c.Unsub(ch1)

// join with context and automatically leave when the context is canceled
ch2, _ := c.Sub(ctx, 1)

// join with 10 subscriber channel buffer
ch3, _ := c.Sub(ctx, 10)

caster can associate with a context as well:

// `c` will be closed when the `ctx` is canceled
c := caster.New(ctx)

A boolean value is returned to indicate whether the caster is still running or not:

_, ok := c.Sub(nil, 1)
if !ok {
    // the caster has been closed, do something else
}

License

MIT

FAQs

Package last updated on 18 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc