{"id":19040771,"url":"https://github.com/cloudfoundry/go-pubsub","last_synced_at":"2025-04-06T12:10:21.067Z","repository":{"id":24405820,"uuid":"101409833","full_name":"cloudfoundry/go-pubsub","owner":"cloudfoundry","description":"Tree based pubsub library for Go.","archived":false,"fork":false,"pushed_at":"2025-03-25T10:42:38.000Z","size":315,"stargazers_count":57,"open_issues_count":2,"forks_count":7,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-03-30T11:07:36.194Z","etag":null,"topics":[],"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/cloudfoundry.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":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-25T14:05:42.000Z","updated_at":"2025-03-25T10:42:35.000Z","dependencies_parsed_at":"2024-06-18T17:07:27.036Z","dependency_job_id":"cf2d99ff-30c1-4490-a2c3-78b062f9b34e","html_url":"https://github.com/cloudfoundry/go-pubsub","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudfoundry%2Fgo-pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudfoundry%2Fgo-pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudfoundry%2Fgo-pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudfoundry%2Fgo-pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudfoundry","download_url":"https://codeload.github.com/cloudfoundry/go-pubsub/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478323,"owners_count":20945266,"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":[],"created_at":"2024-11-08T22:25:06.727Z","updated_at":"2025-04-06T12:10:21.049Z","avatar_url":"https://github.com/cloudfoundry.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n![pubsub][pubsub-logo]\n\n[![GoDoc][go-doc-badge]][go-doc]\n\nPubSub publishes data to subscriptions. However, it can do so much more than\njust push some data to a subscription.  Each subscription is placed in a tree.\nWhen data is published, it traverses the tree and finds each interested\nsubscription. This allows for sophisticated filters and routing.\n\nIf you have any questions, or want to get attention for a PR or issue please reach out on the [#logging-and-metrics channel in the cloudfoundry slack](https://cloudfoundry.slack.com/archives/CUW93AF3M)\n\n### Installation\n\n```bash\ngo get code.cloudfoundry.org/go-pubsub\n```\n\n### Subscription Trees\n\nA subscription tree is a collection of subscriptions that are organized based\non what data they want published to     them. When subscribing, a path is\nprovided to give directions to PubSub about where to store the subscription\nand what data should be published to it.\n\nSo for example, say there are three subscriptions with the following paths:\n\n| Name  | Path              |\n|-------|-------------------|\n| sub-1 | `[\"a\", \"b\", \"c\"]` |\n| sub-2 | `[\"a\", \"b\", \"d\"]` |\n| sub-3 | `[\"a\", \"b\"]`      |\n\nAfter both subscriptions have been registered PubSub will have the following\nsubscription tree:\n```\n              a\n              |\n              b   \u003c-(sub-3)\n             / \\\n(sub-1)-\u003e   c   d   \u003c-(sub-2)\n```\n\nTo better draw out each's subscriptions view of the tree:\n\n##### Sub-1\n\n```\n              a\n              |\n              b\n             /\n(sub-1)-\u003e   c\n```\n\n##### Sub-2\n\n```\n              a\n              |\n              b\n               \\\n                d   \u003c-(sub-2)\n```\n\n##### Sub-3\n\n```\n              a\n              |\n              b   \u003c-(sub-3)\n```\n\nSo to give a few exapmles of how data could be published:\n\n###### Single path\n\n```\n              a\n              |\n              b   \u003c-(sub-3)\n               \\\n                d   \u003c-(sub-2)\n```\n\nIn this example both `sub-2` and `sub-3` would have the data written to it.\n\n###### Multi-Path\n\n```\n              a\n              |\n              b   \u003c-(sub-3)\n             / \\\n(sub-1)-\u003e   c   d   \u003c-(sub-2)\n```\n\nIn this example all `sub-1`, `sub-2` and `sub-3` would have the data written\nto it.\n\n##### Shorter Path\n\n```\n              a\n              |\n              b   \u003c-(sub-3)\n```\n\nIn this example only `sub-3` would have the data written to it.\n\n##### Other Path\n\n```\n              x\n              |\n              y\n```\n\nIn this example, no subscriptions would have data written to them.\n\n### Simple Example:\n\n```go\nps := pubsub.New()\nsubscription := func(name string) pubsub.SubscriptionFunc {\n\treturn func(data interface{}) {\n\t\tfmt.Printf(\"%s -\u003e %v\\n\", name, data)\n\t}\n}\n\nps.Subscribe(subscription(\"sub-1\"), []string{\"a\", \"b\", \"c\"})\nps.Subscribe(subscription(\"sub-2\"), []string{\"a\", \"b\", \"d\"})\nps.Subscribe(subscription(\"sub-3\"), []string{\"a\", \"b\", \"e\"})\n\nps.Publish(\"data-1\", pubsub.LinearTreeTraverser([]string{\"a\", \"b\"}))\nps.Publish(\"data-2\", pubsub.LinearTreeTraverser([]string{\"a\", \"b\", \"c\"}))\nps.Publish(\"data-3\", pubsub.LinearTreeTraverser([]string{\"a\", \"b\", \"d\"}))\nps.Publish(\"data-3\", pubsub.LinearTreeTraverser([]string{\"x\", \"y\"}))\n\n// Output:\n// sub-1 -\u003e data-2\n// sub-2 -\u003e data-3\n```\n\nIn this example the `LinearTreeTraverser` is used to traverse the tree of\nsubscriptions. When an interested subscription is found (in this case `sub-1`\nand `sub-2` for `data-2` and `data-3` respectively), the subscription is\nhanded the data.\n\nMore complex examples can be found in the\n[examples](https://code.cloudfoundry.org/go-pubsub/tree/master/examples)\ndirectory.\n\n### TreeTraversers\n\nA `TreeTraverser` is used to traverse the subscription tree and find what\nsubscriptions should have the data published to them. There are a few\nimplementations provided, however it is likely a user will need to implement\ntheir own to suit their data.\n\nWhen creating a `TreeTraverser` it is important to note how the data is\nstructured. A `TreeTraverser` must be deterministic and ideally stateless. The\norder the data is parsed and returned (via `Traverse()`) must align with the\ngiven path of `Subscribe()`.\n\nThis means if the `TreeTraverser` intends to look at field A, then B, and then\nfinally C, then the subscription path must be A, B and then C (and not B, A, C\nor something).\n\n### Subscriptions\n\nA `Subscription` is used when publishing data. The given path is used to\ndetermine it's placement in the subscription tree.\n\n### Code Generation\n\nThe tree traversers and subscriptions are quite complicated. Laying out a tree\nstructure is not something humans are going to find natural. Therefore a\n[generator](https://github.com/cloudfoundry-incubator/go-pubsub/tree/master/pubsub-gen)\nis provided for structs.\n\nThe struct is inspected (at `go generate` time) and creates the tree layout\ncode. There is a provided\n[example](https://github.com/cloudfoundry-incubator/go-pubsub/tree/master/examples/structs).\n\n[pubsub-logo]:  https://raw.githubusercontent.com/cloudfoundry/go-pubsub/gh-pages/pubsub-logo.png\n[go-doc-badge]: https://godoc.org/code.cloudfoundry.org/go-pubsub?status.svg\n[go-doc]:       https://godoc.org/code.cloudfoundry.org/go-pubsub\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudfoundry%2Fgo-pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudfoundry%2Fgo-pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudfoundry%2Fgo-pubsub/lists"}