{"id":13413394,"url":"https://github.com/asaskevich/EventBus","last_synced_at":"2025-03-14T19:32:09.342Z","repository":{"id":24822771,"uuid":"28237271","full_name":"asaskevich/EventBus","owner":"asaskevich","description":"[Go] Lightweight eventbus with async compatibility for Go","archived":false,"fork":false,"pushed_at":"2024-06-19T06:33:20.000Z","size":47,"stargazers_count":1727,"open_issues_count":26,"forks_count":220,"subscribers_count":31,"default_branch":"master","last_synced_at":"2024-10-15T10:21:04.595Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asaskevich.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2014-12-19T16:38:39.000Z","updated_at":"2024-10-14T11:24:03.000Z","dependencies_parsed_at":"2024-01-08T15:34:40.715Z","dependency_job_id":null,"html_url":"https://github.com/asaskevich/EventBus","commit_stats":{"total_commits":48,"total_committers":20,"mean_commits":2.4,"dds":0.8333333333333334,"last_synced_commit":"49d423059eefd67a7243331db83e16d347139b4a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2FEventBus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2FEventBus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2FEventBus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asaskevich%2FEventBus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asaskevich","download_url":"https://codeload.github.com/asaskevich/EventBus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221498743,"owners_count":16833055,"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-07-30T20:01:39.326Z","updated_at":"2024-10-26T05:30:50.468Z","avatar_url":"https://github.com/asaskevich.png","language":"Go","readme":"EventBus\n======\n\n[![GoDoc](https://godoc.org/github.com/asaskevich/EventBus?status.svg)](https://godoc.org/github.com/asaskevich/EventBus) [![Coverage Status](https://img.shields.io/coveralls/asaskevich/EventBus.svg)](https://coveralls.io/r/asaskevich/EventBus?branch=master) [![Build Status](https://travis-ci.org/asaskevich/EventBus.svg)](https://travis-ci.org/asaskevich/EventBus)\n\nPackage EventBus is the little and lightweight eventbus with async compatibility for GoLang.\n\n#### Installation\nMake sure that Go is installed on your computer.\nType the following command in your terminal:\n\n\tgo get github.com/asaskevich/EventBus\n\nAfter it the package is ready to use.\n\n#### Import package in your project\nAdd following line in your `*.go` file:\n```go\nimport \"github.com/asaskevich/EventBus\"\n```\nIf you unhappy to use long `EventBus`, you can do something like this:\n```go\nimport (\n\tevbus \"github.com/asaskevich/EventBus\"\n)\n```\n\n#### Example\n```go\nfunc calculator(a int, b int) {\n\tfmt.Printf(\"%d\\n\", a + b)\n}\n\nfunc main() {\n\tbus := EventBus.New();\n\tbus.Subscribe(\"main:calculator\", calculator);\n\tbus.Publish(\"main:calculator\", 20, 40);\n\tbus.Unsubscribe(\"main:calculator\", calculator);\n}\n```\n\n#### Implemented methods\n* **New()**\n* **Subscribe()**\n* **SubscribeOnce()**\n* **HasCallback()**\n* **Unsubscribe()**\n* **Publish()**\n* **SubscribeAsync()**\n* **SubscribeOnceAsync()**\n* **WaitAsync()**\n\n#### New()\nNew returns new EventBus with empty handlers.\n```go\nbus := EventBus.New();\n```\n\n#### Subscribe(topic string, fn interface{}) error\nSubscribe to a topic. Returns error if `fn` is not a function.\n```go\nfunc Handler() { ... }\n...\nbus.Subscribe(\"topic:handler\", Handler)\n```\n\n#### SubscribeOnce(topic string, fn interface{}) error\nSubscribe to a topic once. Handler will be removed after executing. Returns error if `fn` is not a function.\n```go\nfunc HelloWorld() { ... }\n...\nbus.SubscribeOnce(\"topic:handler\", HelloWorld)\n```\n\n#### Unsubscribe(topic string, fn interface{}) error\nRemove callback defined for a topic. Returns error if there are no callbacks subscribed to the topic.\n```go\nbus.Unsubscribe(\"topic:handler\", HelloWord);\n```\n\n#### HasCallback(topic string) bool\nReturns true if exists any callback subscribed to the topic.\n\n#### Publish(topic string, args ...interface{})\nPublish executes callback defined for a topic. Any additional argument will be transferred to the callback.\n```go\nfunc Handler(str string) { ... }\n...\nbus.Subscribe(\"topic:handler\", Handler)\n...\nbus.Publish(\"topic:handler\", \"Hello, World!\");\n```\n\n#### SubscribeAsync(topic string, fn interface{}, transactional bool)\nSubscribe to a topic with an asynchronous callback. Returns error if `fn` is not a function.\n```go\nfunc slowCalculator(a, b int) {\n\ttime.Sleep(3 * time.Second)\n\tfmt.Printf(\"%d\\n\", a + b)\n}\n\nbus := EventBus.New()\nbus.SubscribeAsync(\"main:slow_calculator\", slowCalculator, false)\n\nbus.Publish(\"main:slow_calculator\", 20, 60)\n\nfmt.Println(\"start: do some stuff while waiting for a result\")\nfmt.Println(\"end: do some stuff while waiting for a result\")\n\nbus.WaitAsync() // wait for all async callbacks to complete\n\nfmt.Println(\"do some stuff after waiting for result\")\n```\nTransactional determines whether subsequent callbacks for a topic are run serially (true) or concurrently(false)\n\n#### SubscribeOnceAsync(topic string, args ...interface{})\nSubscribeOnceAsync works like SubscribeOnce except the callback to executed asynchronously\n\n####  WaitAsync()\nWaitAsync waits for all async callbacks to complete.\n\n#### Cross Process Events\nWorks with two rpc services:\n- a client service to listen to remotely published events from a server\n- a server service to listen to client subscriptions\n\nserver.go\n```go\nfunc main() {\n    server := NewServer(\":2010\", \"/_server_bus_\", New())\n    server.Start()\n    // ...\n    server.EventBus().Publish(\"main:calculator\", 4, 6)\n    // ...\n    server.Stop()\n}\n```\n\nclient.go\n```go\nfunc main() {\n    client := NewClient(\":2015\", \"/_client_bus_\", New())\n    client.Start()\n    client.Subscribe(\"main:calculator\", calculator, \":2010\", \"/_server_bus_\")\n    // ...\n    client.Stop()\n}\n```\n\n#### Notes\nDocumentation is available here: [godoc.org](https://godoc.org/github.com/asaskevich/EventBus).\nFull information about code coverage is also available here: [EventBus on gocover.io](http://gocover.io/github.com/asaskevich/EventBus).\n\n#### Support\nIf you do have a contribution for the package feel free to put up a Pull Request or open Issue.\n\n#### Special thanks to [contributors](https://github.com/asaskevich/EventBus/graphs/contributors)\n* [Brian Downs](https://github.com/briandowns)\n* [Dominik Schulz](https://github.com/gittex)\n* [bennAH](https://github.com/bennAH)\n* [John Noble] (https://github.com/gaxunil)\n* [Evan Borgstrom] (https://github.com/borgstrom)\n","funding_links":[],"categories":["开源类库","消息","Go","Messaging","Open source library","\u003cspan id=\"消息-messaging\"\u003e消息 Messaging\u003c/span\u003e","消息系统","机器学习","Relational Databases","消息传递"],"sub_categories":["未归类","Advanced Console UIs","Search and Analytic Databases","Not Categorized","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","检索及分析资料库","SQL 查询语句构建库","高级控制台界面","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasaskevich%2FEventBus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasaskevich%2FEventBus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasaskevich%2FEventBus/lists"}