{"id":17963961,"url":"https://github.com/teivah/broadcast","last_synced_at":"2025-10-28T20:06:07.648Z","repository":{"id":43149353,"uuid":"417264864","full_name":"teivah/broadcast","owner":"teivah","description":"Notification broadcaster library","archived":false,"fork":false,"pushed_at":"2022-05-05T07:55:07.000Z","size":189,"stargazers_count":154,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-31T14:11:59.477Z","etag":null,"topics":["channels","concurrency","go","golang","goroutine","goroutines","library","notifications","parallelism","publish-subscribe"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teivah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-14T19:55:37.000Z","updated_at":"2025-03-09T17:01:24.000Z","dependencies_parsed_at":"2022-07-22T00:32:10.212Z","dependency_job_id":null,"html_url":"https://github.com/teivah/broadcast","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fbroadcast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fbroadcast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fbroadcast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teivah%2Fbroadcast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teivah","download_url":"https://codeload.github.com/teivah/broadcast/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247685628,"owners_count":20979085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["channels","concurrency","go","golang","goroutine","goroutines","library","notifications","parallelism","publish-subscribe"],"created_at":"2024-10-29T11:46:01.582Z","updated_at":"2025-10-28T20:06:02.605Z","avatar_url":"https://github.com/teivah.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# broadcast\n\n![CI](https://github.com/teivah/broadcast/actions/workflows/ci.yml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/teivah/broadcast)](https://goreportcard.com/report/github.com/teivah/broadcast)\n\nNotification broadcaster in Go\n\n## What?\n\n`broadcast` is a library that allows sending repeated notifications to multiple goroutines with guaranteed delivery and user defined types.\n\n## Why?\n\n### Why not Channels?\n\nThe standard way to handle notifications is via a `chan struct{}`. However, sending a message to a channel is received by a single goroutine. \n\nThe only operation that is broadcast to multiple goroutines is a channel closure. Yet, if the channel is closed, there's no way to send a message again.\n\n❌ Repeated notifications to multiple goroutines\n\n✅ Guaranteed delivery\n\n### Why not sync.Cond?\n\n`sync.Cond` is the standard solution based on condition variables to set up containers of goroutines waiting for a specific condition.\n\nThere's one caveat to keep in mind, though: the `Broadcast()` method doesn't guarantee that a goroutine will receive the notification. Indeed, the notification will be lost if the listener goroutine isn't waiting on the `Wait()` method.\n\n✅ Repeated notifications to multiple goroutines\n\n❌ Guaranteed delivery\n\n## How?\n\n### Step by Step\n\nFirst, we need to create a `Relay` for a message type (empty struct in this case):\n\n```go\nrelay := broadcast.NewRelay[struct{}]()\n```\n\nOnce a `Relay` is created, we can create a new listener using the `Listener` method. As the `broadcast` library relies internally on channels, it accepts a capacity:\n\n````go\nlist := relay.Listener(1) // Create a new listener based on a channel with a one capacity\n````\n\nA `Relay` can send a notification in three different manners:\n* `Notify`: block until a notification is sent to all the listeners\n* `NotifyCtx`: send a notification to all listeners unless the provided context times out or is canceled\n* `Broadcast`: send a notification to all listeners in a non-blocking manner; delivery isn't guaranteed\n\nOn the `Listener` side, we can access the internal channel using `Ch`:\n\n```go\n\u003c-list.Ch() // Wait on a notification\n```\n\nWe can close a `Listener` and a `Relay` using `Close`:\n\n```go\nlist.Close() \nrelay.Close()\n```\n\nClosing a `Relay` and `Listener`s can be done concurrently in a safe manner.\n\n### Example\n\n```go\ntype msg string\nconst (\n    msgA msg = \"A\"\n    msgB     = \"B\"\n    msgC     = \"C\"\n)\n\nrelay := broadcast.NewRelay[msg]() // Create a relay for msg values\ndefer relay.Close()\n\n// Listener goroutines\nfor i := 0; i \u003c 2; i++ {\n    go func(i int) {\n        l := relay.Listener(1)  // Create a listener with a buffer capacity of 1\n        for n := range l.Ch() { // Ranges over notifications\n            fmt.Printf(\"listener %d has received a notification: %v\\n\", i, n)\n        }\n    }(i)\n}\n\n// Notifiers\ntime.Sleep(time.Second)\nrelay.Notify(msgA)                                     // Send notification with guaranteed delivery\nctx, _ := context.WithTimeout(context.Background(), 0) // Context with immediate timeout\nrelay.NotifyCtx(ctx, msgB)                             // Send notification respecting context cancellation\ntime.Sleep(time.Second)                                // Allow time for previous messages to be processed\nrelay.Broadcast(msgC)                                  // Send notification without guaranteed delivery\ntime.Sleep(time.Second)                                // Allow time for previous messages to be processed\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteivah%2Fbroadcast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteivah%2Fbroadcast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteivah%2Fbroadcast/lists"}